亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用一個空的dataframe-cbine.fill綁定一個dataframe?

用一個空的dataframe-cbine.fill綁定一個dataframe?

慕尼黑8549860 2019-07-16 10:13:23
用一個空的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 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

但是,我認為泰勒的解決方案是直接和最好的,我只是提供了另一種方式,使用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))))


查看完整回答
反對 回復 2019-07-16
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

使用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


查看完整回答
反對 回復 2019-07-16
  • 3 回答
  • 0 關注
  • 850 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號