Reputation: 1170
My first data frame (a sample of a larger one) has 10 rows and 13 columns. The second one has 4 rows and 13 columns (All except for the Month
are NA
s.
When I try to rbind
them I get the next error:
Error in rbind(deparse.level, ...) : replacement has length zero
And I can't figure out what's the matter, since they all have identical column names.
dput:
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")
Upvotes: 3
Views: 2216
Reputation: 78590
The problem is that you have several columns that are themselves data frames in sample.df
. E.g.:
class(sample.df$Intention_SPD)
# "data.frame"
All of the columns in data1
are atomic vectors. To fix this, you could turn all of the columns of sample.df
into vectors with do.call(data.frame, sample.df)
. Thus, this works:
rbind(do.call(data.frame, sample.df), data1)
Upvotes: 5