我有一个存储两个数据帧的列表
data <- list(structure(list(GROUP = "3\nN=6 (86%)", col1 = "6.5 [5.25; 7.75]",
col2 = "9 [7.25; 10.75]", col3 = "10.5 [9.25; 11.75]", dif = "1-2: -3 [-3; -3]\n1-3: -5 [-5; -5]\n2-3: -2 [-2; -2]",
p.value = "1-2: 0.107\n1-3: 0.013*\n2-3: 0.295"), row.names = c(NA,
-1L), class = "data.frame"), structure(list(GROUP = "3\nN=6 (86%)",
col3 = "10.5 [9.25; 11.75]", col4 = "14.5 [12.5; 15.75]",
col5 = "16.5 [15.25; 17.75]", dif = "3-4: -3 [-3; -3]\n3-5: -6 [-6; -6]\n4-5: -3 [-3; -3]",
p.value = "3-4: 0.030*\n3-5: 0.002*\n4-5: 0.126"), row.names = c(NA,
-1L), class = "data.frame"))我想把它们组合起来,得到一个带有头的数据帧。我想得到的是:
data <- structure(list(GROUP = c("3\nN=6 (86%)", "GROUP", "3\nN=6 (86%)"
), col1 = c("6.5 [5.25; 7.75]", "col3", "10.5 [9.25; 11.75]"),
col2 = c("9 [7.25; 10.75]", "col4", "14.5 [12.5; 15.75]"),
col3 = c("10.5 [9.25; 11.75]", "col5", "16.5 [15.25; 17.75]"
), dif = c("1-2: -3 [-3; -3]\n1-3: -5 [-5; -5]\n2-3: -2 [-2; -2]",
"dif", "3-4: -3 [-3; -3]\n3-5: -6 [-6; -6]\n4-5: -3 [-3; -3]"
), p.value = c("1-2: 0.107\n1-3: 0.013*\n2-3: 0.295", "p.value",
"3-4: 0.030*\n3-5: 0.002*\n4-5: 0.126")), row.names = c(NA,
-3L), class = "data.frame")我试着用:
rbindlist(data);
do.call(rbind;data)
lapply(data, setNames, names(data))但这些都帮不了我
发布于 2022-11-15 05:22:35
使用do调用
然后将第一行转换为列,然后删除第一行。
tt<- do.call(rbind.data.frame, lapply(data, function(x) unname(rbind(names(x), as.matrix(x)))))
names(tt) <- tt[1,]
tt <- tt[-1,]https://stackoverflow.com/questions/74440689
复制相似问题