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?
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
add a comment |
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
4
L = mget(c("a1", "a2", "a3")); lapply(L, intersect, my.numbers)
, I guess.
– Frank
Mar 7 at 20:38
add a comment |
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
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
r
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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"
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 7 at 21:21
camillecamille
7,76631833
7,76631833
add a comment |
add a comment |
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"
add a comment |
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"
add a comment |
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"
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"
answered Mar 7 at 21:14
ChaboChabo
1,1981823
1,1981823
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
4
L = mget(c("a1", "a2", "a3")); lapply(L, intersect, my.numbers)
, I guess.– Frank
Mar 7 at 20:38