【发布时间】:2021-06-26 22:48:41
【问题描述】:
我正在 rbind将 data.frames 列表 (k) 转换为单个 data.frame (current_output)。
但我想知道如何将 id 列添加到我的结果 data.frame 中,以便在 BASE R 中获得我的 desred_output?
k = list(A = data.frame(d = 1, n = 2), B = data.frame(d = 1:2, n = 2:3))
current_output = do.call(rbind, k)
# d n
#A 1 2
#B.1 1 2
#B.2 2 3
desired_ouput1 = data.frame(id = c(1,2,2), d = c(1,1:2), n = c(2,2:3))
# id d n
#1 1 1 2
#2 2 1 2
#3 2 2 3
desired_ouput2 = data.frame(id = c(A,B,B), d = c(1,1:2), n = c(2,2:3))
# id d n
#1 A 1 2
#2 B 1 2
#3 B 2 3
【问题讨论】:
-
@Henrik,可以提供 BASE R 解决方案吗?非常感谢。
标签: r list dataframe loops lapply