我的数据输出是一个列表,其中包含参与者数据的嵌套列表。
DF = list(list(list(0.601705399641698)), list(list(1.0967226417789),
list(1.18794820947846), list(3.98557480186904)), list(list(
0.995996328131446), list(0.564513260465204), list(1.31356753668681),
list(0.891872221784271)), list(list(0.649008813684839), list(
1.0036615512612), list(NA_real_)), list(list(0.828273303379322)))
当我使用map_df合并这些列表时,它会像这样移动行:
xtest = map_df(DF, ~as.data.frame(.x), .null = NA)
# which comes out like this
structure(list(X0.601705399641698 = c(0.601705399641698, NA,
NA, NA, NA), X1.0967226417789 = c(NA, 1.0967226417789, NA, NA,
NA), X1.18794820947846 = c(NA, 1.18794820947846, NA, NA, NA),
X3.98557480186904 = c(NA, 3.98557480186904, NA, NA, NA),
X0.995996328131446 = c(NA, NA, 0.995996328131446, NA, NA),
X0.564513260465204 = c(NA, NA, 0.564513260465204, NA, NA),
X1.31356753668681 = c(NA, NA, 1.31356753668681, NA, NA),
X0.891872221784271 = c(NA, NA, 0.891872221784271, NA, NA),
X0.649008813684839 = c(NA, NA, NA, 0.649008813684839, NA),
X1.0036615512612 = c(NA, NA, NA, 1.0036615512612, NA), NA_real_. = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), X0.828273303379322 = c(NA,
NA, NA, NA, 0.828273303379322)), row.names = c(NA, -5L), class = "data.frame")
X0.601705399641698 X1.0967226417789 X1.18794820947846 X3.98557480186904 X0.995996328131446 X0.564513260465204 X1.31356753668681 X0.891872221784271
1 0.6017054 NA NA NA NA NA NA NA
2 NA 1.096723 1.187948 3.985575 NA NA NA NA
3 NA NA NA NA 0.9959963 0.5645133 1.313568 0.8918722
4 NA NA NA NA NA NA NA NA
5 NA NA NA NA NA NA NA NA
X0.649008813684839 X1.0036615512612 NA_real_. X0.828273303379322
1 NA NA NA NA
2 NA NA NA NA
3 NA NA NA NA
4 0.6490088 1.003662 NA NA
5 NA NA NA 0.8282733
似乎是按列添加数据,并转移新值,而不是从参与者2的第1列第2行开始,依此类推。
是否有办法将map_df数据帧输出更改为从第1列开始,但保持与每个参与者对应的行?
预期输出:
1 2 3 4
1 0.6017054 NA NA NA # participant 1
2 1.096723 1.187948 3.985575 NA # participant 2
3 0.9959963 0.5645133 1.313568 0.8918722 # participant 3
4 0.6490088 1.003662 NA NA # Participant 4
5 0.8282733 NA NA NA # Participant 5
答案 0 :(得分:0)
一种tidyverse
方法
library(tidyverse)
map_dfr(DF, ~unlist(.x) %>% t() %>% as.data.frame())
# V1 V2 V3 V4
#1 0.6017054 NA NA NA
#2 1.0967226 1.1879482 3.985575 NA
#3 0.9959963 0.5645133 1.313568 0.8918722
#4 0.6490088 1.0036616 NA NA
#5 0.8282733 NA NA NA
或使用data.table
library(data.table)
rbindlist(lapply(DF, function(x) data.table(t(unlist(x)))), fill = TRUE)
# V1 V2 V3 V4
#1: 0.6017054 NA NA NA
#2: 1.0967226 1.1879482 3.985575 NA
#3: 0.9959963 0.5645133 1.313568 0.8918722
#4: 0.6490088 1.0036616 NA NA
#5: 0.8282733 NA NA NA