2015-11-12 39 views
3

我的第一個數據幀(較大的樣本)有10行和13列。第二個具有4行13列(所有除了MonthNA小號合併具有相同列數的數據幀

當我嘗試rbind他們,我得到了一個錯誤:

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

,我想不出有什麼這件事,因爲它們都具有相同的列名

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") 
+2

您準確使用什麼代碼來合併您的數據? – Heroka

+0

@Heroka'df1 < - rbind(sample.df,data1)' – Zlo

+0

請按行和列逐列減少數據行,直到獲得仍顯示錯誤的最小案例。幸運的是,你會自己發現問題,如果不是,那麼你將有一個很好的乾淨問題,讓別人分流。 –

回答

5

的問題是,你有幾列是在sample.df自己數據幀。例如:

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

data1中的所有列都是原子向量。要解決這個問題,您可以將sample.df的所有列轉換爲do.call(data.frame, sample.df)的矢量。因此,它的工作原理如下:

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

謝謝,這非常有幫助! – Zlo

+0

我認爲'rbind(do.call(data.frame,sample.df),data1)'在這裏是正確的調用。雖然他們都工作。 –

+0

@PierreLafortune你是對的,謝謝 –

相關問題