Reputation: 8364
I have 3 lists
, each with n
elements of length fixed (10 in the example):
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:
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".
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"
Upvotes: 2
Views: 76
Reputation: 79208
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)
Upvotes: 3
Reputation: 17648
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
Upvotes: 2
Reputation: 33488
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
Made more generalisable with predefined, n
variables.
EDIT2: Benchmark
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
Upvotes: 3