我有以下列表:
> 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
所以我们可以说3属于组1,6属于组2,7属于组3,依此类推。我需要反向映射,即到每个我想拥有的组ID的数字(请参见预期的输出):
> 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
我认为purrr::transpose
应该可以完成工作,但是它并没有完全按照我的意图进行,是吗?怎么做到呢?
PS。最终,我只需要一个形式为3 2 1 1 1 2 3 1 2 2
的向量,但在上述之上,我认为unlist()
就足以转换。
答案 0 :(得分:3)
这是一个基本解决方案...
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))]
答案 1 :(得分:2)
我可以建议一个老式的循环吗?
# 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
答案 2 :(得分:1)
检查此解决方案:
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()
答案 3 :(得分:1)
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
以下3种形式将获得相同的输出:
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)
答案 4 :(得分:1)
也在base
中,类似
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)
与l <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
。
或包装在函数中
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
# ...
答案 5 :(得分:0)
使用purrr
的解决方案。 dat2
是最终输出,一个整数向量。
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
答案 6 :(得分:0)
使用 char *scene1 =
" \n"
" \n"
" \n"
" \n"
" \n"
" \n"
" * \n"
" * * \n"
" * * \n";
char *scene2 =
" * \n"
" * \n"
" * \n"
" * \n"
" * \n"
" * \n"
" * \n"
" * * \n"
" * * \n";
和tidyverse
,我们可以创建一个purr::imap_dfr
,其值和索引并排,tibble
按值和{{ 1}}索引:
arrange
在基数R中翻译得很少(输出相同):
pull