【发布时间】:2018-01-27 00:13:06
【问题描述】:
假设这是我的列表结构
lst=list(structure(c("level1", "level2", "level4", "level5","18", "abc", "pqr", "lmn"),
.Dim = c(4L, 2L)),
structure(c("level1", "level2", "level3", "level5", "level6", "20", "xyz", "hive", "foo", "bar"),
.Dim = c(5L, 2L)),
structure(c("level1", "level3", "level4", "level5","level6", "22", "dark", "yellow","foobar", "blue"),
.Dim = c(5L, 2L)),
structure(c("level1", "level2", "level3", "level5","level6","level7","24", "dvd", "dxs","glass", "while","though"),
.Dim = c(6L, 2L))
)
期待这样的 O/P
level1 level2 level3 level4 level5 level6 level7
1) 18 abc NA pqr lmn NA NA
2) 20 xyz hive NA foo bar NA
3) 22 NA dark yellow foobar blue NA
4) 24 dvd dxs NA glass while though
所有列表的第一列应该被转置,相应的数据应该被查找到它们的行。
尝试将所有行转置到列本身给出错误
unique(t(list_temp[[c(1,2)]][,1]))
ERROR:Error in list_temp[[c(1, 2)]][, 1] : incorrect number of dimensions
也试过
apply(list_temp,1,function(x){list_temp[[x]][,1]})
但是给了我
Error in apply(list_temp, 1, function(x) { :
dim(X) must have a positive length
关于如何完成的任何建议。
谢谢。
【问题讨论】:
标签: r list dataframe lapply transpose