gpt4 book ai didi

r - 合并具有相同列数的数据框

转载 作者:行者123 更新时间:2023-12-01 11:29:42 34 4
gpt4 key购买 nike

我的第一个数据框(较大数据框的样本)有 10 行和 13 列。第二个有 4 行和 13 列(除了 Month 之外的所有内容都是 NA

当我尝试 rbind 它们时,出现下一个错误:

Error in rbind(deparse.level, ...) : replacement has length zero

我不知道怎么回事,因为它们都有相同的列名。

输入:

    dput(sample.df)
structure(list(Month = structure(c(8674, 8552, 8401, 8491, 8521,
8460, 8644, 8432, 8705, 8582), class = "Date"), Intention_CDU = c(211L,
240L, 246L, 232L, 261L, 222L, 234L, 223L, 249L, 241L), Intention_SPD = structure(list(
Intention_SPD = c(296L, 290L, 304L, 274L, 238L, 276L, 284L,
323L, 324L, 291L)), .Names = "Intention_SPD", row.names = c(9L,
6L, 1L, 4L, 5L, 3L, 8L, 2L, 10L, 7L), class = "data.frame"),
Intention_FDP = structure(list(Intention_FDP = c(40L, 50L,
47L, 36L, 35L, 46L, 33L, 44L, 33L, 31L)), .Names = "Intention_FDP", row.names = c(9L,
6L, 1L, 4L, 5L, 3L, 8L, 2L, 10L, 7L), class = "data.frame"),
Intention_Green = structure(list(Intention_Green = c(97L,
93L, 112L, 97L, 92L, 108L, 131L, 90L, 100L, 80L)), .Names = "Intention_Green", row.names = c(9L,
6L, 1L, 4L, 5L, 3L, 8L, 2L, 10L, 7L), class = "data.frame"),
Intention_PDS = structure(list(Intention_PDS = c(1L, 4L,
1L, 4L, 2L, 1L, 3L, 2L, 1L, 6L)), .Names = "Intention_PDS", row.names = c(9L,
6L, 1L, 4L, 5L, 3L, 8L, 2L, 10L, 7L), class = "data.frame"),
Intention_Right = structure(list(Intention_Right = c(39L,
26L, 40L, 44L, 48L, 51L, 33L, 45L, 27L, 30L)), .Names = "Intention_Right", row.names = c(9L,
6L, 1L, 4L, 5L, 3L, 8L, 2L, 10L, 7L), class = "data.frame"),
CDU_scalometer = c(5.67605633802817, 5.8090241343127, 5.65452755905512,
5.79253112033195, 6.15352260778128, 5.61145194274029, 5.86511156186613,
5.56134969325153, 5.82591093117409, 5.78158458244111), CSU_scalometer = c(5.26910994764398,
5.2734375, 5.22417355371901, 5.16648411829135, 5.48986486486486,
5.05206073752711, 5.55080213903743, 5.07593582887701, 5.29957805907173,
5.35327963176064), FDP_scalometer = c(5.66122448979592, 5.66666666666667,
5.32698094282849, 5.32563025210084, 5.75965665236051, 5.51706308169597,
5.36663233779609, 5.73606729758149, 5.33991683991684, 5.67868852459016
), PDS_scalometer = c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), Grüne_scalmeter = c(6.2864476386037, 5.687432867884,
5.814, 6.00941422594142, 5.82429501084599, 6.2239263803681,
6.42443064182195, 6.128125, 5.90476190476191, 5.72203765227021
), SPD_scalometer = c(7.13104838709677, 6.60669456066946,
6.7509842519685, 6.53478712357217, 6.33019853709509, 6.37307297019527,
7.16818642350557, 7.09304703476483, 6.94939271255061, 6.7258064516129
)), .Names = c("Month", "Intention_CDU", "Intention_SPD",
"Intention_FDP", "Intention_Green", "Intention_PDS", "Intention_Right",
"CDU_scalometer", "CSU_scalometer", "FDP_scalometer", "PDS_scalometer",
"Grüne_scalmeter", "SPD_scalometer"), row.names = c(9L, 6L,
1L, 4L, 5L, 3L, 8L, 2L, 10L, 7L), class = "data.frame")

.

dput(data1)
structure(list(Month = structure(c(8613, 9343, 9678, 10043), class = "Date"),
Intention_CDU = c(NA, NA, NA, NA), Intention_SPD = c(NA,
NA, NA, NA), Intention_FDP = c(NA, NA, NA, NA), Intention_Green = c(NA,
NA, NA, NA), Intention_PDS = c(NA, NA, NA, NA), Intention_Right = c(NA,
NA, NA, NA), CDU_scalometer = c(NA, NA, NA, NA), CSU_scalometer = c(NA,
NA, NA, NA), FDP_scalometer = c(NA, NA, NA, NA), PDS_scalometer = c(NA,
NA, NA, NA), Grüne_scalmeter = c(NA, NA, NA, NA), SPD_scalometer = c(NA,
NA, NA, NA)), .Names = c("Month", "Intention_CDU", "Intention_SPD",
"Intention_FDP", "Intention_Green", "Intention_PDS", "Intention_Right",
"CDU_scalometer", "CSU_scalometer", "FDP_scalometer", "PDS_scalometer",
"Grüne_scalmeter", "SPD_scalometer"), row.names = c(NA, -4L), class = "data.frame")

最佳答案

问题是您在 sample.df 中有几列它们自己 数据帧。例如:

class(sample.df$Intention_SPD)
# "data.frame"

data1 中的所有列都是原子向量。要解决此问题,您可以使用 do.call(data.frame, sample.df)sample.df 的所有列转换为向量。因此,这有效:

rbind(do.call(data.frame, sample.df), data1)

关于r - 合并具有相同列数的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33680584/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com