用一個空的dataframe-cbine.fill綁定一個dataframe?我想我在找一個模擬的rbind.fill(在Hadley‘splyr(包裝)cbind..我看過了,但沒有cbind.fill.我想做的是:#set these just for this exampleone_option <- TRUEdiff_option <- TRUEreturn_df <- data.frame()if (one_option) {
#do a bunch of calculations, produce a data.frame, for simplicity the following small_df
small_df <- data.frame(a=1, b=2)
return_df <- cbind(return_df,small_df)}if (diff_option) {
#do a bunch of calculations, produce a data.frame, for simplicity the following small2_df
small2_df <- data.frame(l="hi there", m=44)
return_df <- cbind(return_df,small2_df)}return_df可以理解,這會產生一個錯誤:Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 0, 1我現在的修復方法是替換線路return_df <- data.frame()帶著return_df <- data.frame(dummy=1)然后代碼就能工作了。然后,我只需從return_df最后。在添加虛擬代碼并運行上述代碼之后,我得到 dummy a b l m1 1 1 2 hi there 44然后我只需要處理掉假人,例如:> return_df[,2:ncol(return_df)]
a b l m1 1 2 hi there 44我肯定我錯過了一個更簡單的方法來做這件事。編輯:我想我不是在找Cbinn,因為這意味著安娜的值將在cbind之后創建,這不是我想要的。
3 回答

catspeake
TA貢獻1111條經驗 獲得超0個贊
我只是提供了另一種方式,使用rbind.fill()
我們已經有了。
require(plyr)?#?requires?plyr?for?rbind.fill()cbind.fill?<-?function(...)?{??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??transpoted?<-?lapply(list(...),t)????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??transpoted_dataframe?<-?lapply(transpoted,?as.data.frame)????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??return?(data.frame(t(rbind.fill(transpoted_dataframe))))

繁花不似錦
TA貢獻1851條經驗 獲得超4個贊
使用rowr::cbind.fill
rowr::cbind.fill(df1,df2,fill = NA)
A B
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 NA 6
添加回答
舉報
0/150
提交
取消