How to turn a list inside out?
I've got a following list:
> list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
[[1]]
[1] 3 4 5 8
[[2]]
[1] 2 6 9 10
[[3]]
[1] 1 7
So we can say that 3 belongs to group 1, 6 belongs to group 2, 7 belongs to group 3 and so on. I need a reverse mapping, i.e. to every number I want to have a group id that it belongs to (see expected output):
> list(3, 2, 1, 1, 1, 2, 3, 1, 2, 2)
[[1]]
[1] 3
[[2]]
[1] 2
[[3]]
[1] 1
[[4]]
[1] 1
[[5]]
[1] 1
[[6]]
[1] 2
[[7]]
[1] 3
[[8]]
[1] 1
[[9]]
[1] 2
[[10]]
[1] 2
I thought purrr::transpose
should do the job but it doesn't exactly do what I intend, is it? How can it be done?
PS. Ultimately, I need just a vector of a form: 3 2 1 1 1 2 3 1 2 2
, but having above I think unlist()
is enough to convert.
r base purrr
add a comment |
I've got a following list:
> list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
[[1]]
[1] 3 4 5 8
[[2]]
[1] 2 6 9 10
[[3]]
[1] 1 7
So we can say that 3 belongs to group 1, 6 belongs to group 2, 7 belongs to group 3 and so on. I need a reverse mapping, i.e. to every number I want to have a group id that it belongs to (see expected output):
> list(3, 2, 1, 1, 1, 2, 3, 1, 2, 2)
[[1]]
[1] 3
[[2]]
[1] 2
[[3]]
[1] 1
[[4]]
[1] 1
[[5]]
[1] 1
[[6]]
[1] 2
[[7]]
[1] 3
[[8]]
[1] 1
[[9]]
[1] 2
[[10]]
[1] 2
I thought purrr::transpose
should do the job but it doesn't exactly do what I intend, is it? How can it be done?
PS. Ultimately, I need just a vector of a form: 3 2 1 1 1 2 3 1 2 2
, but having above I think unlist()
is enough to convert.
r base purrr
My apologies but I can't make out how to get3 2 1 1 1 2 3 1 2 2
from given input, Its atleast not clear to me. Thanks
– PKumar
Jan 13 at 7:03
@PKumar: 1 is in group 3, 2 is in group 2, 3 is in group 1, 4 is in group 1, 5 is in group 1 ...
– Khaynes
Jan 13 at 7:08
1
We start from 1 and see which group 1 belongs to - we can see it's in 3rd group, namely 3rd element of a list. Then we go with 2:10 and check the same.
– jakes
Jan 13 at 7:08
add a comment |
I've got a following list:
> list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
[[1]]
[1] 3 4 5 8
[[2]]
[1] 2 6 9 10
[[3]]
[1] 1 7
So we can say that 3 belongs to group 1, 6 belongs to group 2, 7 belongs to group 3 and so on. I need a reverse mapping, i.e. to every number I want to have a group id that it belongs to (see expected output):
> list(3, 2, 1, 1, 1, 2, 3, 1, 2, 2)
[[1]]
[1] 3
[[2]]
[1] 2
[[3]]
[1] 1
[[4]]
[1] 1
[[5]]
[1] 1
[[6]]
[1] 2
[[7]]
[1] 3
[[8]]
[1] 1
[[9]]
[1] 2
[[10]]
[1] 2
I thought purrr::transpose
should do the job but it doesn't exactly do what I intend, is it? How can it be done?
PS. Ultimately, I need just a vector of a form: 3 2 1 1 1 2 3 1 2 2
, but having above I think unlist()
is enough to convert.
r base purrr
I've got a following list:
> list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
[[1]]
[1] 3 4 5 8
[[2]]
[1] 2 6 9 10
[[3]]
[1] 1 7
So we can say that 3 belongs to group 1, 6 belongs to group 2, 7 belongs to group 3 and so on. I need a reverse mapping, i.e. to every number I want to have a group id that it belongs to (see expected output):
> list(3, 2, 1, 1, 1, 2, 3, 1, 2, 2)
[[1]]
[1] 3
[[2]]
[1] 2
[[3]]
[1] 1
[[4]]
[1] 1
[[5]]
[1] 1
[[6]]
[1] 2
[[7]]
[1] 3
[[8]]
[1] 1
[[9]]
[1] 2
[[10]]
[1] 2
I thought purrr::transpose
should do the job but it doesn't exactly do what I intend, is it? How can it be done?
PS. Ultimately, I need just a vector of a form: 3 2 1 1 1 2 3 1 2 2
, but having above I think unlist()
is enough to convert.
r base purrr
r base purrr
asked Jan 13 at 6:51
jakesjakes
409311
409311
My apologies but I can't make out how to get3 2 1 1 1 2 3 1 2 2
from given input, Its atleast not clear to me. Thanks
– PKumar
Jan 13 at 7:03
@PKumar: 1 is in group 3, 2 is in group 2, 3 is in group 1, 4 is in group 1, 5 is in group 1 ...
– Khaynes
Jan 13 at 7:08
1
We start from 1 and see which group 1 belongs to - we can see it's in 3rd group, namely 3rd element of a list. Then we go with 2:10 and check the same.
– jakes
Jan 13 at 7:08
add a comment |
My apologies but I can't make out how to get3 2 1 1 1 2 3 1 2 2
from given input, Its atleast not clear to me. Thanks
– PKumar
Jan 13 at 7:03
@PKumar: 1 is in group 3, 2 is in group 2, 3 is in group 1, 4 is in group 1, 5 is in group 1 ...
– Khaynes
Jan 13 at 7:08
1
We start from 1 and see which group 1 belongs to - we can see it's in 3rd group, namely 3rd element of a list. Then we go with 2:10 and check the same.
– jakes
Jan 13 at 7:08
My apologies but I can't make out how to get
3 2 1 1 1 2 3 1 2 2
from given input, Its atleast not clear to me. Thanks– PKumar
Jan 13 at 7:03
My apologies but I can't make out how to get
3 2 1 1 1 2 3 1 2 2
from given input, Its atleast not clear to me. Thanks– PKumar
Jan 13 at 7:03
@PKumar: 1 is in group 3, 2 is in group 2, 3 is in group 1, 4 is in group 1, 5 is in group 1 ...
– Khaynes
Jan 13 at 7:08
@PKumar: 1 is in group 3, 2 is in group 2, 3 is in group 1, 4 is in group 1, 5 is in group 1 ...
– Khaynes
Jan 13 at 7:08
1
1
We start from 1 and see which group 1 belongs to - we can see it's in 3rd group, namely 3rd element of a list. Then we go with 2:10 and check the same.
– jakes
Jan 13 at 7:08
We start from 1 and see which group 1 belongs to - we can see it's in 3rd group, namely 3rd element of a list. Then we go with 2:10 and check the same.
– jakes
Jan 13 at 7:08
add a comment |
7 Answers
7
active
oldest
votes
Here is a base solution ...
list <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
rep(1:length(list), sapply(list, length))[order(unlist(list))]
3
Nice solution, you could fully vectorize this usingrep(seq_along(l), lengths(l))[order(unlist(l))]
(not sure what you are usingas.numeric
for).
– David Arenburg
2 days ago
2
Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)
– Henrik
2 days ago
@DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))
– Khaynes
2 days ago
add a comment |
Can I suggest an old-fashioned loop:
# your list
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
# the data in the list as vector
num <- unlist( x )
# the variable that will be the position vector
pos <- NULL
# loop through the possible position, see which number it contains
# find which "group it belongs to, and add that finding to the position vector
for( i in 1:length( num ) )
for( j in 1:length( x ) )
if( i %in% x[[j]] ) pos <- c( pos, j )
pos
[1] 3 2 1 1 1 2 3 1 2 2
add a comment |
Also in base
, something like this
L <- as.list(setNames( rep(1:length(lengths(l)), lengths(l)), unlist(l)))
# if wanted, sort it with
L[as.character(sort(as.integer(names(L))))]
# if wanted, unname with
unname(L)
with l <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
.
Or wrapped in a function
list_inside_out <- function (l, unName = TRUE) {
l2 <- lengths(l)
out <- as.list(setNames(rep(1:length(l2), l2), unlist(l)))
out <- out[as.character(sort(as.integer(names(out))))]
if (unName) return(unname(out))
out
}
list_inside_out(l)
# [[1]]
# [1] 3
#
# [[2]]
# [1] 2
#
# [[3]]
# [1] 1
# ...
add a comment |
Check this solution:
library(tidyverse)
library(magrittr)
library(wrapr)
list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7)) %.>%
tibble(x = .) %>%
mutate(rn = row_number()) %>%
unnest() %>%
arrange(x) %$%
set_names(rn, x) %>%
as.list()
New contributor
Is the 5th line a mistake%.>%
?
– Khaynes
2 days ago
No. It's a pipe fromwrapr
package.
– Paweł Chabros
2 days ago
1
Why do you use%.>%
in wrapr additionally instead of using%>%
in tidyverse? They will get the same outcome in this case.
– Darren Tsai
2 days ago
You are right. My bad.
– Paweł Chabros
2 days ago
add a comment |
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
Following 3 forms will get the same outputs:
library(tidyverse)
# (1)
x %>% set_names(1:3) %>% stack %>% arrange(values) %>% select(ind)
# (2)
x %>% enframe %>% unnest %>% arrange(value) %>% select(name)
# (3)
x %>% (reshape2::melt) %>% arrange(value) %>% select(L1)
add a comment |
A solution using purrr
. dat2
is the final output, an integer vector.
dat <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
library(purrr)
dat2 <- dat %>%
imap(~set_names(.x, rep(.y, length(.x)))) %>%
unlist() %>%
sort() %>%
names() %>%
as.integer()
dat2
# [1] 3 2 1 1 1 2 3 1 2 2
add a comment |
Using tidyverse
and purr::imap_dfr
we can create a tibble
with the values and indices side by side, arrange
by value and pull
the indices :
list_ <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
library(tidyverse)
imap_dfr(list_,~tibble(.x,.y)) %>% arrange(.x) %>% pull(.y) %>% as.list
# [[1]]
# [1] 3
#
# [[2]]
# [1] 2
#
# [[3]]
# [1] 1
#
# [[4]]
# [1] 1
#
# [[5]]
# [1] 1
#
# [[6]]
# [1] 2
#
# [[7]]
# [1] 3
#
# [[8]]
# [1] 1
#
# [[9]]
# [1] 2
#
# [[10]]
# [1] 2
Less pretty translated in base R (same output) :
with(
as.data.frame(do.call(rbind,Map(cbind,a = list_, b =seq_along(list_)))),
as.list(b[order(a)]))
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%2f54166704%2fhow-to-turn-a-list-inside-out%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a base solution ...
list <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
rep(1:length(list), sapply(list, length))[order(unlist(list))]
3
Nice solution, you could fully vectorize this usingrep(seq_along(l), lengths(l))[order(unlist(l))]
(not sure what you are usingas.numeric
for).
– David Arenburg
2 days ago
2
Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)
– Henrik
2 days ago
@DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))
– Khaynes
2 days ago
add a comment |
Here is a base solution ...
list <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
rep(1:length(list), sapply(list, length))[order(unlist(list))]
3
Nice solution, you could fully vectorize this usingrep(seq_along(l), lengths(l))[order(unlist(l))]
(not sure what you are usingas.numeric
for).
– David Arenburg
2 days ago
2
Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)
– Henrik
2 days ago
@DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))
– Khaynes
2 days ago
add a comment |
Here is a base solution ...
list <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
rep(1:length(list), sapply(list, length))[order(unlist(list))]
Here is a base solution ...
list <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
rep(1:length(list), sapply(list, length))[order(unlist(list))]
edited 2 days ago
answered Jan 13 at 7:12
KhaynesKhaynes
443417
443417
3
Nice solution, you could fully vectorize this usingrep(seq_along(l), lengths(l))[order(unlist(l))]
(not sure what you are usingas.numeric
for).
– David Arenburg
2 days ago
2
Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)
– Henrik
2 days ago
@DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))
– Khaynes
2 days ago
add a comment |
3
Nice solution, you could fully vectorize this usingrep(seq_along(l), lengths(l))[order(unlist(l))]
(not sure what you are usingas.numeric
for).
– David Arenburg
2 days ago
2
Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)
– Henrik
2 days ago
@DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))
– Khaynes
2 days ago
3
3
Nice solution, you could fully vectorize this using
rep(seq_along(l), lengths(l))[order(unlist(l))]
(not sure what you are using as.numeric
for).– David Arenburg
2 days ago
Nice solution, you could fully vectorize this using
rep(seq_along(l), lengths(l))[order(unlist(l))]
(not sure what you are using as.numeric
for).– David Arenburg
2 days ago
2
2
Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)
– Henrik
2 days ago
Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)
– Henrik
2 days ago
@DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))
– Khaynes
2 days ago
@DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))
– Khaynes
2 days ago
add a comment |
Can I suggest an old-fashioned loop:
# your list
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
# the data in the list as vector
num <- unlist( x )
# the variable that will be the position vector
pos <- NULL
# loop through the possible position, see which number it contains
# find which "group it belongs to, and add that finding to the position vector
for( i in 1:length( num ) )
for( j in 1:length( x ) )
if( i %in% x[[j]] ) pos <- c( pos, j )
pos
[1] 3 2 1 1 1 2 3 1 2 2
add a comment |
Can I suggest an old-fashioned loop:
# your list
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
# the data in the list as vector
num <- unlist( x )
# the variable that will be the position vector
pos <- NULL
# loop through the possible position, see which number it contains
# find which "group it belongs to, and add that finding to the position vector
for( i in 1:length( num ) )
for( j in 1:length( x ) )
if( i %in% x[[j]] ) pos <- c( pos, j )
pos
[1] 3 2 1 1 1 2 3 1 2 2
add a comment |
Can I suggest an old-fashioned loop:
# your list
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
# the data in the list as vector
num <- unlist( x )
# the variable that will be the position vector
pos <- NULL
# loop through the possible position, see which number it contains
# find which "group it belongs to, and add that finding to the position vector
for( i in 1:length( num ) )
for( j in 1:length( x ) )
if( i %in% x[[j]] ) pos <- c( pos, j )
pos
[1] 3 2 1 1 1 2 3 1 2 2
Can I suggest an old-fashioned loop:
# your list
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
# the data in the list as vector
num <- unlist( x )
# the variable that will be the position vector
pos <- NULL
# loop through the possible position, see which number it contains
# find which "group it belongs to, and add that finding to the position vector
for( i in 1:length( num ) )
for( j in 1:length( x ) )
if( i %in% x[[j]] ) pos <- c( pos, j )
pos
[1] 3 2 1 1 1 2 3 1 2 2
answered 2 days ago
vaettchenvaettchen
5,1701332
5,1701332
add a comment |
add a comment |
Also in base
, something like this
L <- as.list(setNames( rep(1:length(lengths(l)), lengths(l)), unlist(l)))
# if wanted, sort it with
L[as.character(sort(as.integer(names(L))))]
# if wanted, unname with
unname(L)
with l <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
.
Or wrapped in a function
list_inside_out <- function (l, unName = TRUE) {
l2 <- lengths(l)
out <- as.list(setNames(rep(1:length(l2), l2), unlist(l)))
out <- out[as.character(sort(as.integer(names(out))))]
if (unName) return(unname(out))
out
}
list_inside_out(l)
# [[1]]
# [1] 3
#
# [[2]]
# [1] 2
#
# [[3]]
# [1] 1
# ...
add a comment |
Also in base
, something like this
L <- as.list(setNames( rep(1:length(lengths(l)), lengths(l)), unlist(l)))
# if wanted, sort it with
L[as.character(sort(as.integer(names(L))))]
# if wanted, unname with
unname(L)
with l <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
.
Or wrapped in a function
list_inside_out <- function (l, unName = TRUE) {
l2 <- lengths(l)
out <- as.list(setNames(rep(1:length(l2), l2), unlist(l)))
out <- out[as.character(sort(as.integer(names(out))))]
if (unName) return(unname(out))
out
}
list_inside_out(l)
# [[1]]
# [1] 3
#
# [[2]]
# [1] 2
#
# [[3]]
# [1] 1
# ...
add a comment |
Also in base
, something like this
L <- as.list(setNames( rep(1:length(lengths(l)), lengths(l)), unlist(l)))
# if wanted, sort it with
L[as.character(sort(as.integer(names(L))))]
# if wanted, unname with
unname(L)
with l <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
.
Or wrapped in a function
list_inside_out <- function (l, unName = TRUE) {
l2 <- lengths(l)
out <- as.list(setNames(rep(1:length(l2), l2), unlist(l)))
out <- out[as.character(sort(as.integer(names(out))))]
if (unName) return(unname(out))
out
}
list_inside_out(l)
# [[1]]
# [1] 3
#
# [[2]]
# [1] 2
#
# [[3]]
# [1] 1
# ...
Also in base
, something like this
L <- as.list(setNames( rep(1:length(lengths(l)), lengths(l)), unlist(l)))
# if wanted, sort it with
L[as.character(sort(as.integer(names(L))))]
# if wanted, unname with
unname(L)
with l <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
.
Or wrapped in a function
list_inside_out <- function (l, unName = TRUE) {
l2 <- lengths(l)
out <- as.list(setNames(rep(1:length(l2), l2), unlist(l)))
out <- out[as.character(sort(as.integer(names(out))))]
if (unName) return(unname(out))
out
}
list_inside_out(l)
# [[1]]
# [1] 3
#
# [[2]]
# [1] 2
#
# [[3]]
# [1] 1
# ...
edited 2 days ago
answered 2 days ago
natenate
1,877316
1,877316
add a comment |
add a comment |
Check this solution:
library(tidyverse)
library(magrittr)
library(wrapr)
list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7)) %.>%
tibble(x = .) %>%
mutate(rn = row_number()) %>%
unnest() %>%
arrange(x) %$%
set_names(rn, x) %>%
as.list()
New contributor
Is the 5th line a mistake%.>%
?
– Khaynes
2 days ago
No. It's a pipe fromwrapr
package.
– Paweł Chabros
2 days ago
1
Why do you use%.>%
in wrapr additionally instead of using%>%
in tidyverse? They will get the same outcome in this case.
– Darren Tsai
2 days ago
You are right. My bad.
– Paweł Chabros
2 days ago
add a comment |
Check this solution:
library(tidyverse)
library(magrittr)
library(wrapr)
list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7)) %.>%
tibble(x = .) %>%
mutate(rn = row_number()) %>%
unnest() %>%
arrange(x) %$%
set_names(rn, x) %>%
as.list()
New contributor
Is the 5th line a mistake%.>%
?
– Khaynes
2 days ago
No. It's a pipe fromwrapr
package.
– Paweł Chabros
2 days ago
1
Why do you use%.>%
in wrapr additionally instead of using%>%
in tidyverse? They will get the same outcome in this case.
– Darren Tsai
2 days ago
You are right. My bad.
– Paweł Chabros
2 days ago
add a comment |
Check this solution:
library(tidyverse)
library(magrittr)
library(wrapr)
list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7)) %.>%
tibble(x = .) %>%
mutate(rn = row_number()) %>%
unnest() %>%
arrange(x) %$%
set_names(rn, x) %>%
as.list()
New contributor
Check this solution:
library(tidyverse)
library(magrittr)
library(wrapr)
list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7)) %.>%
tibble(x = .) %>%
mutate(rn = row_number()) %>%
unnest() %>%
arrange(x) %$%
set_names(rn, x) %>%
as.list()
New contributor
New contributor
answered Jan 13 at 7:29
Paweł ChabrosPaweł Chabros
30915
30915
New contributor
New contributor
Is the 5th line a mistake%.>%
?
– Khaynes
2 days ago
No. It's a pipe fromwrapr
package.
– Paweł Chabros
2 days ago
1
Why do you use%.>%
in wrapr additionally instead of using%>%
in tidyverse? They will get the same outcome in this case.
– Darren Tsai
2 days ago
You are right. My bad.
– Paweł Chabros
2 days ago
add a comment |
Is the 5th line a mistake%.>%
?
– Khaynes
2 days ago
No. It's a pipe fromwrapr
package.
– Paweł Chabros
2 days ago
1
Why do you use%.>%
in wrapr additionally instead of using%>%
in tidyverse? They will get the same outcome in this case.
– Darren Tsai
2 days ago
You are right. My bad.
– Paweł Chabros
2 days ago
Is the 5th line a mistake
%.>%
?– Khaynes
2 days ago
Is the 5th line a mistake
%.>%
?– Khaynes
2 days ago
No. It's a pipe from
wrapr
package.– Paweł Chabros
2 days ago
No. It's a pipe from
wrapr
package.– Paweł Chabros
2 days ago
1
1
Why do you use
%.>%
in wrapr additionally instead of using %>%
in tidyverse? They will get the same outcome in this case.– Darren Tsai
2 days ago
Why do you use
%.>%
in wrapr additionally instead of using %>%
in tidyverse? They will get the same outcome in this case.– Darren Tsai
2 days ago
You are right. My bad.
– Paweł Chabros
2 days ago
You are right. My bad.
– Paweł Chabros
2 days ago
add a comment |
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
Following 3 forms will get the same outputs:
library(tidyverse)
# (1)
x %>% set_names(1:3) %>% stack %>% arrange(values) %>% select(ind)
# (2)
x %>% enframe %>% unnest %>% arrange(value) %>% select(name)
# (3)
x %>% (reshape2::melt) %>% arrange(value) %>% select(L1)
add a comment |
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
Following 3 forms will get the same outputs:
library(tidyverse)
# (1)
x %>% set_names(1:3) %>% stack %>% arrange(values) %>% select(ind)
# (2)
x %>% enframe %>% unnest %>% arrange(value) %>% select(name)
# (3)
x %>% (reshape2::melt) %>% arrange(value) %>% select(L1)
add a comment |
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
Following 3 forms will get the same outputs:
library(tidyverse)
# (1)
x %>% set_names(1:3) %>% stack %>% arrange(values) %>% select(ind)
# (2)
x %>% enframe %>% unnest %>% arrange(value) %>% select(name)
# (3)
x %>% (reshape2::melt) %>% arrange(value) %>% select(L1)
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
Following 3 forms will get the same outputs:
library(tidyverse)
# (1)
x %>% set_names(1:3) %>% stack %>% arrange(values) %>% select(ind)
# (2)
x %>% enframe %>% unnest %>% arrange(value) %>% select(name)
# (3)
x %>% (reshape2::melt) %>% arrange(value) %>% select(L1)
edited 2 days ago
answered 2 days ago
Darren TsaiDarren Tsai
1,567321
1,567321
add a comment |
add a comment |
A solution using purrr
. dat2
is the final output, an integer vector.
dat <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
library(purrr)
dat2 <- dat %>%
imap(~set_names(.x, rep(.y, length(.x)))) %>%
unlist() %>%
sort() %>%
names() %>%
as.integer()
dat2
# [1] 3 2 1 1 1 2 3 1 2 2
add a comment |
A solution using purrr
. dat2
is the final output, an integer vector.
dat <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
library(purrr)
dat2 <- dat %>%
imap(~set_names(.x, rep(.y, length(.x)))) %>%
unlist() %>%
sort() %>%
names() %>%
as.integer()
dat2
# [1] 3 2 1 1 1 2 3 1 2 2
add a comment |
A solution using purrr
. dat2
is the final output, an integer vector.
dat <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
library(purrr)
dat2 <- dat %>%
imap(~set_names(.x, rep(.y, length(.x)))) %>%
unlist() %>%
sort() %>%
names() %>%
as.integer()
dat2
# [1] 3 2 1 1 1 2 3 1 2 2
A solution using purrr
. dat2
is the final output, an integer vector.
dat <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
library(purrr)
dat2 <- dat %>%
imap(~set_names(.x, rep(.y, length(.x)))) %>%
unlist() %>%
sort() %>%
names() %>%
as.integer()
dat2
# [1] 3 2 1 1 1 2 3 1 2 2
answered 2 days ago
wwwwww
26.1k112240
26.1k112240
add a comment |
add a comment |
Using tidyverse
and purr::imap_dfr
we can create a tibble
with the values and indices side by side, arrange
by value and pull
the indices :
list_ <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
library(tidyverse)
imap_dfr(list_,~tibble(.x,.y)) %>% arrange(.x) %>% pull(.y) %>% as.list
# [[1]]
# [1] 3
#
# [[2]]
# [1] 2
#
# [[3]]
# [1] 1
#
# [[4]]
# [1] 1
#
# [[5]]
# [1] 1
#
# [[6]]
# [1] 2
#
# [[7]]
# [1] 3
#
# [[8]]
# [1] 1
#
# [[9]]
# [1] 2
#
# [[10]]
# [1] 2
Less pretty translated in base R (same output) :
with(
as.data.frame(do.call(rbind,Map(cbind,a = list_, b =seq_along(list_)))),
as.list(b[order(a)]))
add a comment |
Using tidyverse
and purr::imap_dfr
we can create a tibble
with the values and indices side by side, arrange
by value and pull
the indices :
list_ <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
library(tidyverse)
imap_dfr(list_,~tibble(.x,.y)) %>% arrange(.x) %>% pull(.y) %>% as.list
# [[1]]
# [1] 3
#
# [[2]]
# [1] 2
#
# [[3]]
# [1] 1
#
# [[4]]
# [1] 1
#
# [[5]]
# [1] 1
#
# [[6]]
# [1] 2
#
# [[7]]
# [1] 3
#
# [[8]]
# [1] 1
#
# [[9]]
# [1] 2
#
# [[10]]
# [1] 2
Less pretty translated in base R (same output) :
with(
as.data.frame(do.call(rbind,Map(cbind,a = list_, b =seq_along(list_)))),
as.list(b[order(a)]))
add a comment |
Using tidyverse
and purr::imap_dfr
we can create a tibble
with the values and indices side by side, arrange
by value and pull
the indices :
list_ <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
library(tidyverse)
imap_dfr(list_,~tibble(.x,.y)) %>% arrange(.x) %>% pull(.y) %>% as.list
# [[1]]
# [1] 3
#
# [[2]]
# [1] 2
#
# [[3]]
# [1] 1
#
# [[4]]
# [1] 1
#
# [[5]]
# [1] 1
#
# [[6]]
# [1] 2
#
# [[7]]
# [1] 3
#
# [[8]]
# [1] 1
#
# [[9]]
# [1] 2
#
# [[10]]
# [1] 2
Less pretty translated in base R (same output) :
with(
as.data.frame(do.call(rbind,Map(cbind,a = list_, b =seq_along(list_)))),
as.list(b[order(a)]))
Using tidyverse
and purr::imap_dfr
we can create a tibble
with the values and indices side by side, arrange
by value and pull
the indices :
list_ <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
library(tidyverse)
imap_dfr(list_,~tibble(.x,.y)) %>% arrange(.x) %>% pull(.y) %>% as.list
# [[1]]
# [1] 3
#
# [[2]]
# [1] 2
#
# [[3]]
# [1] 1
#
# [[4]]
# [1] 1
#
# [[5]]
# [1] 1
#
# [[6]]
# [1] 2
#
# [[7]]
# [1] 3
#
# [[8]]
# [1] 1
#
# [[9]]
# [1] 2
#
# [[10]]
# [1] 2
Less pretty translated in base R (same output) :
with(
as.data.frame(do.call(rbind,Map(cbind,a = list_, b =seq_along(list_)))),
as.list(b[order(a)]))
edited yesterday
answered yesterday
Moody_MudskipperMoody_Mudskipper
21.7k32864
21.7k32864
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%2f54166704%2fhow-to-turn-a-list-inside-out%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
My apologies but I can't make out how to get
3 2 1 1 1 2 3 1 2 2
from given input, Its atleast not clear to me. Thanks– PKumar
Jan 13 at 7:03
@PKumar: 1 is in group 3, 2 is in group 2, 3 is in group 1, 4 is in group 1, 5 is in group 1 ...
– Khaynes
Jan 13 at 7:08
1
We start from 1 and see which group 1 belongs to - we can see it's in 3rd group, namely 3rd element of a list. Then we go with 2:10 and check the same.
– jakes
Jan 13 at 7:08