Find out where the given numbers are located in the series of vector in RTest if a vector contains a given elementRscript: Determine path of the executing scriptCounting the number of elements with the values of x in a vectorDrop data frame columns by nameHow to disable scientific notation?How to unload a package without restarting R?Extracting specific columns from a data frameHow to find the amount of positive numbersHow can I view the source code for a function?data.table vs dplyr: can one do something well the other can't or does poorly?

How do you justify more code being written by following clean code practices?

Would a primitive species be able to learn English from reading books alone?

Friend wants my recommendation but I don't want to give it to him

Reasons for having MCU pin-states default to pull-up/down out of reset

Can you describe someone as luxurious? As in someone who likes luxurious things?

A seasonal riddle

Can a Knock spell open the door to Mordenkainen's Magnificent Mansion?

How to get directions in deep space?

Showing mass murder in a kid's book

Asserting that Atheism and Theism are both faith based positions

Connection Between Knot Theory and Number Theory

Put the phone down / Put down the phone

"Oh no!" in Latin

Output visual diagram of picture

Offset in split text content

Could a welfare state co-exist with mega corporations?

Should a narrator ever describe things based on a character's view instead of facts?

Checking @@ROWCOUNT failing

What (if any) is the reason to buy in small local stores?

Rendered textures different to 3D View

Highest stage count that are used one right after the other?

Make a Bowl of Alphabet Soup

Why does the frost depth increase when the surface temperature warms up?

Using an older 200A breaker panel on a 60A feeder circuit from house?



Find out where the given numbers are located in the series of vector in R


Test if a vector contains a given elementRscript: Determine path of the executing scriptCounting the number of elements with the values of x in a vectorDrop data frame columns by nameHow to disable scientific notation?How to unload a package without restarting R?Extracting specific columns from a data frameHow to find the amount of positive numbersHow can I view the source code for a function?data.table vs dplyr: can one do something well the other can't or does poorly?













0















I have three variables a1, a2 and a3.



a1 <- 1:10
a2 <- 11:20
a3 <- 21:30


then I have another variable called my.numbers <- c(1, 20, 22,11)



I want to find out where these numbers are located. So the result I want is:



1 in a1
20 in a2
22 in a3
11 in a2


Any suggestion on how it can be done easy way?










share|improve this question

















  • 4





    L = mget(c("a1", "a2", "a3")); lapply(L, intersect, my.numbers), I guess.

    – Frank
    Mar 7 at 20:38















0















I have three variables a1, a2 and a3.



a1 <- 1:10
a2 <- 11:20
a3 <- 21:30


then I have another variable called my.numbers <- c(1, 20, 22,11)



I want to find out where these numbers are located. So the result I want is:



1 in a1
20 in a2
22 in a3
11 in a2


Any suggestion on how it can be done easy way?










share|improve this question

















  • 4





    L = mget(c("a1", "a2", "a3")); lapply(L, intersect, my.numbers), I guess.

    – Frank
    Mar 7 at 20:38













0












0








0








I have three variables a1, a2 and a3.



a1 <- 1:10
a2 <- 11:20
a3 <- 21:30


then I have another variable called my.numbers <- c(1, 20, 22,11)



I want to find out where these numbers are located. So the result I want is:



1 in a1
20 in a2
22 in a3
11 in a2


Any suggestion on how it can be done easy way?










share|improve this question














I have three variables a1, a2 and a3.



a1 <- 1:10
a2 <- 11:20
a3 <- 21:30


then I have another variable called my.numbers <- c(1, 20, 22,11)



I want to find out where these numbers are located. So the result I want is:



1 in a1
20 in a2
22 in a3
11 in a2


Any suggestion on how it can be done easy way?







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 20:34









MAPKMAPK

1,797835




1,797835







  • 4





    L = mget(c("a1", "a2", "a3")); lapply(L, intersect, my.numbers), I guess.

    – Frank
    Mar 7 at 20:38












  • 4





    L = mget(c("a1", "a2", "a3")); lapply(L, intersect, my.numbers), I guess.

    – Frank
    Mar 7 at 20:38







4




4





L = mget(c("a1", "a2", "a3")); lapply(L, intersect, my.numbers), I guess.

– Frank
Mar 7 at 20:38





L = mget(c("a1", "a2", "a3")); lapply(L, intersect, my.numbers), I guess.

– Frank
Mar 7 at 20:38












2 Answers
2






active

oldest

votes


















1














With a couple purrr::map functions, you can work across the numbers, then within that, across the a vectors.



I'm making a list of the a vectors with tibble::lst because it sets the names of the list as the names of the variables going into it—convenient for something like this where it's the name of the list item that's important.





library(tidyverse)

a_list <- lst(a1, a2, a3)

my.numbers %>%
map_chr(function(num)
which_a <- map_lgl(a_list, ~(num %in% .))
a_name <- names(a_list)[which_a]
str_glue("num in a_name")
)
#> [1] "1 in a1" "20 in a2" "22 in a3" "11 in a2"


You could use match or another function after the map_lgl instead—I left it verbose to make it a little more clear what's going on.






share|improve this answer






























    1














    For the record here is how you can get out the exact result in the question.



    a1 <- 1:10
    a2 <- 11:20
    a3 <- 21:30

    L<-list("a1"=a1,"a2"=a2,"a3"=a3)
    my.numbers <- c(1, 20, 22, 11)

    func<-function(item)
    my.numbers[which(my.numbers %in% item)]


    Fin<-lapply(L, func)

    for(i in 1:length(Fin))
    Index<-unlist(Fin[i])
    name<-paste("a",i, sep="")
    for(i in 1:length(Index))
    print(paste(Index[i], "in", name))



    [1] "1 in a1"
    [1] "20 in a2"
    [1] "11 in a2"
    [1] "22 in a3"





    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55052382%2ffind-out-where-the-given-numbers-are-located-in-the-series-of-vector-in-r%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      With a couple purrr::map functions, you can work across the numbers, then within that, across the a vectors.



      I'm making a list of the a vectors with tibble::lst because it sets the names of the list as the names of the variables going into it—convenient for something like this where it's the name of the list item that's important.





      library(tidyverse)

      a_list <- lst(a1, a2, a3)

      my.numbers %>%
      map_chr(function(num)
      which_a <- map_lgl(a_list, ~(num %in% .))
      a_name <- names(a_list)[which_a]
      str_glue("num in a_name")
      )
      #> [1] "1 in a1" "20 in a2" "22 in a3" "11 in a2"


      You could use match or another function after the map_lgl instead—I left it verbose to make it a little more clear what's going on.






      share|improve this answer



























        1














        With a couple purrr::map functions, you can work across the numbers, then within that, across the a vectors.



        I'm making a list of the a vectors with tibble::lst because it sets the names of the list as the names of the variables going into it—convenient for something like this where it's the name of the list item that's important.





        library(tidyverse)

        a_list <- lst(a1, a2, a3)

        my.numbers %>%
        map_chr(function(num)
        which_a <- map_lgl(a_list, ~(num %in% .))
        a_name <- names(a_list)[which_a]
        str_glue("num in a_name")
        )
        #> [1] "1 in a1" "20 in a2" "22 in a3" "11 in a2"


        You could use match or another function after the map_lgl instead—I left it verbose to make it a little more clear what's going on.






        share|improve this answer

























          1












          1








          1







          With a couple purrr::map functions, you can work across the numbers, then within that, across the a vectors.



          I'm making a list of the a vectors with tibble::lst because it sets the names of the list as the names of the variables going into it—convenient for something like this where it's the name of the list item that's important.





          library(tidyverse)

          a_list <- lst(a1, a2, a3)

          my.numbers %>%
          map_chr(function(num)
          which_a <- map_lgl(a_list, ~(num %in% .))
          a_name <- names(a_list)[which_a]
          str_glue("num in a_name")
          )
          #> [1] "1 in a1" "20 in a2" "22 in a3" "11 in a2"


          You could use match or another function after the map_lgl instead—I left it verbose to make it a little more clear what's going on.






          share|improve this answer













          With a couple purrr::map functions, you can work across the numbers, then within that, across the a vectors.



          I'm making a list of the a vectors with tibble::lst because it sets the names of the list as the names of the variables going into it—convenient for something like this where it's the name of the list item that's important.





          library(tidyverse)

          a_list <- lst(a1, a2, a3)

          my.numbers %>%
          map_chr(function(num)
          which_a <- map_lgl(a_list, ~(num %in% .))
          a_name <- names(a_list)[which_a]
          str_glue("num in a_name")
          )
          #> [1] "1 in a1" "20 in a2" "22 in a3" "11 in a2"


          You could use match or another function after the map_lgl instead—I left it verbose to make it a little more clear what's going on.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 21:21









          camillecamille

          7,76631833




          7,76631833























              1














              For the record here is how you can get out the exact result in the question.



              a1 <- 1:10
              a2 <- 11:20
              a3 <- 21:30

              L<-list("a1"=a1,"a2"=a2,"a3"=a3)
              my.numbers <- c(1, 20, 22, 11)

              func<-function(item)
              my.numbers[which(my.numbers %in% item)]


              Fin<-lapply(L, func)

              for(i in 1:length(Fin))
              Index<-unlist(Fin[i])
              name<-paste("a",i, sep="")
              for(i in 1:length(Index))
              print(paste(Index[i], "in", name))



              [1] "1 in a1"
              [1] "20 in a2"
              [1] "11 in a2"
              [1] "22 in a3"





              share|improve this answer



























                1














                For the record here is how you can get out the exact result in the question.



                a1 <- 1:10
                a2 <- 11:20
                a3 <- 21:30

                L<-list("a1"=a1,"a2"=a2,"a3"=a3)
                my.numbers <- c(1, 20, 22, 11)

                func<-function(item)
                my.numbers[which(my.numbers %in% item)]


                Fin<-lapply(L, func)

                for(i in 1:length(Fin))
                Index<-unlist(Fin[i])
                name<-paste("a",i, sep="")
                for(i in 1:length(Index))
                print(paste(Index[i], "in", name))



                [1] "1 in a1"
                [1] "20 in a2"
                [1] "11 in a2"
                [1] "22 in a3"





                share|improve this answer

























                  1












                  1








                  1







                  For the record here is how you can get out the exact result in the question.



                  a1 <- 1:10
                  a2 <- 11:20
                  a3 <- 21:30

                  L<-list("a1"=a1,"a2"=a2,"a3"=a3)
                  my.numbers <- c(1, 20, 22, 11)

                  func<-function(item)
                  my.numbers[which(my.numbers %in% item)]


                  Fin<-lapply(L, func)

                  for(i in 1:length(Fin))
                  Index<-unlist(Fin[i])
                  name<-paste("a",i, sep="")
                  for(i in 1:length(Index))
                  print(paste(Index[i], "in", name))



                  [1] "1 in a1"
                  [1] "20 in a2"
                  [1] "11 in a2"
                  [1] "22 in a3"





                  share|improve this answer













                  For the record here is how you can get out the exact result in the question.



                  a1 <- 1:10
                  a2 <- 11:20
                  a3 <- 21:30

                  L<-list("a1"=a1,"a2"=a2,"a3"=a3)
                  my.numbers <- c(1, 20, 22, 11)

                  func<-function(item)
                  my.numbers[which(my.numbers %in% item)]


                  Fin<-lapply(L, func)

                  for(i in 1:length(Fin))
                  Index<-unlist(Fin[i])
                  name<-paste("a",i, sep="")
                  for(i in 1:length(Index))
                  print(paste(Index[i], "in", name))



                  [1] "1 in a1"
                  [1] "20 in a2"
                  [1] "11 in a2"
                  [1] "22 in a3"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 7 at 21:14









                  ChaboChabo

                  1,1981823




                  1,1981823



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55052382%2ffind-out-where-the-given-numbers-are-located-in-the-series-of-vector-in-r%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                      Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                      2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived