【发布时间】:2021-06-04 08:29:38
【问题描述】:
我有一个名为 results1 的元素列表。每个元素都有一个列名和行名。这些元素是向量。
class(results1)
[1] "list"
> class(results1[[1]])
[1] "numeric"
is.vector(results1[[1]])
[1] TRUE
results1[[1]]
..a15.pdf ..a17.pdf ..a18.pdf ..a21.pdf
1.27679608 1.05090176 1.51820192 2.30296037
我想将每个向量转换为包含两列的数据框,其中行名(上面表示为 ..a15.pdf 等)是其中一列。我可以用一个单独的元素/向量来做到这一点:
j<-as.data.frame(results1[[1]])
j<-cbind(j,names=rownames(j))
rownames(j) <- c()
但我无法为整个列表实现它。我试过了
results1<-lapply(results1, function(x)cbind(x, names=rownames(x)))
我可以看到两列
results1[[1]]
x
..a15.pdf 1.27679608
..a17.pdf 1.05090176
..a18.pdf 1.51820192
但显然这不会创建数据框。
is.data.frame(results1[[1]])
[1] FALSE
据我了解,..15pdf 不是一列
results1[[1]][,1]
..a15.pdf ..a17.pdf ..a18.pdf ..a21.pdf
1.27679608 1.05090176 1.51820192 2.30296037
然后我尝试先将我的向量转换为数据帧:
results1<-lapply(results1, function(x)as.data.frame(x[1]))
results1<-lapply(results1, function(x){as.data.frame(x[1])})
但它没有工作。 如上所述,我想将每个向量的行名转换为一列并删除它们。输出应该是一个数据帧列表,每个 df 有两列。如果您需要名称,则 rownames 列应命名为“名称”。谢谢。 这是我列表的前两个向量的 dput
dput(results1[c(1,2)])
list(c(..a15.pdf = 1.27679607834331, ..a17.pdf = 1.05090175857491,
..a18.pdf = 1.51820192474905, ..a21.pdf = 2.30296037386815, ..a2TTT.pdf = 1.48568731934637,
..a5.pdf = 0.493713103224402, ..B11.pdf = 1.02705905465749, ..B12.pdf = 0.999747360884078,
..B13.pdf = 2.40828101927852, ..B22.pdf = 0.695152132033603,
..B24.pdf = 2.1436001615064, ..B4.pdf = 2.25444037842867, ..B7.pdf = 0.909773940025014,
..B8.pdf = 1.14837173756827, `..cw10-1.pdf` = -1.36323271003293,
`..cw15-1TTT.pdf` = 0.341428535787024, `..cw17-1.pdf` = -0.786878348480425,
..cw18.pdf = 0.793720472787986, ..cw3.pdf = -1.57831038567642,
..cw4.pdf = 0.277733503122777, ..cw7_1TTT.pdf = -0.0364645818969112,
`..cw13-1.pdf` = -18.336668416705), c(..a15.pdf = 2.096687569578,
..a17.pdf = 2.19826038300833, ..a18.pdf = 1.91814204277357, ..a21.pdf = 0.801448541154512,
..a2TTT.pdf = 2.16169560949165, ..a5.pdf = 1.48585130705963,
..B11.pdf = 0.95126061691997, ..B12.pdf = 1.93116618236938, ..B13.pdf = 1.92555316191766,
..B22.pdf = 1.00560861920225, ..B24.pdf = 2.91129684208931, ..B4.pdf = 2.75687804718002,
..B7.pdf = 1.31164431967781, ..B8.pdf = 2.22449059765255, `..cw10-1.pdf` = -1.22629519335285,
`..cw15-1TTT.pdf` = 1.31168579553008, `..cw17-1.pdf` = -17.5786422399896,
..cw18.pdf = 1.25323523754693, ..cw3.pdf = -0.754445550651364,
..cw4.pdf = 0.555577381430987, ..cw7_1TTT.pdf = 0.577850999404076,
`..cw13-1.pdf` = -34.2662973287062))
【问题讨论】:
标签: r