[英]Combine n list in a list of data.frames
I have 3 lists
, each with n
elements of length fixed (10 in the example): 我有3个
lists
,每个lists
都有n
固定长度的元素(示例中为10个):
my_list <- list(1:10,11:20,21:30)
my_list2 <- list(31:40,41:50,51:60)
my_list3 <- list(rep("a", 10), rep("b", 10), rep("c", 10))
I'd like to put them in a list
of data.frames
, combining "vertically" each list element: 我想将它们放在
data.frames
list
中,将每个列表元素“垂直”组合:
final <- list(
tb1 = data.frame(v1=1:10, v2=31:40, v3=rep("a",10)),
tb2 = data.frame(v1=11:20, v2=41:50, v3=rep("b", 10)),
tb3 = data.frame(v1=21:30, v2=51:60, v3=rep("c", 10))
)
# map(final, head, 2)
# $`tb1`
# v1 v2 v3
# 1 1 31 a
# 2 2 32 a
#
# $tb2
# v1 v2 v3
# 1 11 41 b
# 2 12 42 b
#
# $tb3
# v1 v2 v3
# 1 21 51 c
# 2 22 52 c
With c
and rbind
at least I combined them in the right order, but I can't think of a way to put them in a data.frame
"every 3 elements". 至少用
c
和rbind
我按正确的顺序组合它们,但我想不出把它们放在data.frame
“每3个元素”的方法。
ab <- c(rbind(my_list, my_list2, my_list3))
# head(ab,3)
# [[1]]
# [1] 1 2 3 4 5 6 7 8 9 10
#
# [[2]]
# [1] 31 32 33 34 35 36 37 38 39 40
#
# [[3]]
# [1] "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"
You could something hackish like this: 你可能会像这样破解:
n_lists <- 3 # length(ls(pattern = "my_list"))
n_cols_each <- 4 # length(get(ls(pattern = "my_list")[[1]]))
temp <- do.call(data.frame, mget(ls(pattern = "my_list")))
final <- lapply(
1:n_lists,
function(x) setNames(temp[x + (0:(n_cols_each-1))*3], paste0("v", 1:n_cols_each))
)
lapply(final, head, 2)
[[1]]
v1 v2 v3
1 1 31 a
2 2 32 a
[[2]]
v1 v2 v3
1 11 41 b
2 12 42 b
[[3]]
v1 v2 v3
1 21 51 c
2 22 52 c
EDITED EDITED
Made more generalisable with predefined, n
variables. 使用预定义的
n
变量更加通用。
EDIT2: Benchmark EDIT2:基准
bench::mark(
snoram = {
n_lists <- 3
n_cols_each <- 3
temp <- do.call(data.frame, list(my_list, my_list2, my_list3))
final <- lapply(
1:n_lists,
function(x) setNames(temp[x + (0:(n_cols_each-1))*3], paste0("v", 1:3))
)
NULL
},
Jimbou = {
res <- list()
for(i in 1:3) {
res[[i]] <- data.frame(V1=my_list[[i]], V2=my_list2[[i]], V3=my_list3[[i]])
}
},
ELrico = {
ll <- list(my_list,my_list2,my_list3)
final <-
lapply(seq_along(ll), function(x){ structure(as.data.frame(
sapply(seq_along(ll[[1]]), function(u) { ll[[u]][x] })
), .Names = paste0("v",seq_along(ll))) })
NULL
}
)
# A tibble: 3 x 14
expression min mean median max `itr/sec` mem_alloc n_gc n_itr
<chr> <bch:> <bch:> <bch:> <bch:> <dbl> <bch:byt> <dbl> <int>
1 snoram 1.13ms 1.49ms 1.33ms 2.68ms 673. 34.9KB 6 297
2 Jimbou 3.87ms 4.37ms 4.25ms 8.46ms 229. 20.3KB 7 86
3 ELrico 1.17ms 1.38ms 1.3ms 2.9ms 725. 26.7KB 7 315
There are various solutions: 有各种解决方案:
library(tidyverse)
map(transpose(mget(ls(pattern = 'my_list'))),~set_names(as.tibble(.x),paste0("V",1:length(.x))))
You can also do 你也可以
split.default(map_dfc(mget(ls(pattern='my_list')),~.x),f=row(diag(3)))
Also you can decide to form a grouped dataframe: 您还可以决定形成分组数据框:
map_df(set_names(transpose(mget(ls(pattern = 'my_list'))),1:3),~.x)
You can try also this 你也可以试试这个
res <- list()
for(i in 1:3) {
res[[i]] <- data.frame(V1=my_list[[i]], V2=my_list2[[i]], V3=my_list3[[i]])
}
map(res, head, 2)
[[1]]
V1 V2 V3
1 1 31 a
2 2 32 a
[[2]]
V1 V2 V3
1 11 31 a
2 12 32 a
[[3]]
V1 V2 V3
1 21 31 a
2 22 32 a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.