用空df(cbind.fill?)绑定df

我想我正在寻找一个类似于rbind.fill (哈德利的plyr包)的cbind 。 我看了,但没有cbind.fill

我想要做的是以下几点:

 #set these just for this example one_option <- TRUE diff_option <- TRUE return_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(dummy=1)replace行return_df <- data.frame() return_df <- data.frame(dummy=1) ,然后代码工作。 然后,我只是从最后的return_df删除虚拟。 添加虚拟并运行上面的代码后,我得到了

  dummy ablm 1 1 1 2 hi there 44 

那么我只需要摆脱假人,例如:

 > return_df[,2:ncol(return_df)] ablm 1 1 2 hi there 44 

我确定我错过了一个更简单的方法来做到这一点。

编辑:我想我不是在寻找一个cbind.fill,因为这意味着一个NA值会在cbind之后创build,这不是我想要的。

这是一个cbind填充:

 cbind.fill <- function(...){ nm <- list(...) nm <- lapply(nm, as.matrix) n <- max(sapply(nm, nrow)) do.call(cbind, lapply(nm, function (x) rbind(x, matrix(, n-nrow(x), ncol(x))))) } 

让我们试试看:

 x<-matrix(1:10,5,2) y<-matrix(1:16, 4,4) z<-matrix(1:12, 2,6) cbind.fill(x,y) cbind.fill(x,y,z) cbind.fill(mtcars, mtcars[1:10,]) 

我想我是从某个地方偷的。

从这里编辑页面: 链接

虽然,我认为泰勒的解决scheme是直接和最好的,我只是用另一种方式,使用我们已经有的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)))) } 

来自qpcR软件包的cbind.na可以做到这一点。

  install.packages("qpcR") library(qpcR) qpcR:::cbind.na(1, 1:7) 

我build议修改泰勒的答案。 我的函数允许在不丢失列名的情况下使用向量绑定数据框架和/或matrix,就像在Tyler的解决scheme中发生的那样

 cbind.fill <- function(...){ nm <- list(...) dfdetect <- grepl("data.frame|matrix", unlist(lapply(nm, function(cl) paste(class(cl), collapse = " ") ))) # first cbind vectors together vec <- data.frame(nm[!dfdetect]) n <- max(sapply(nm[dfdetect], nrow)) vec <- data.frame(lapply(vec, function(x) rep(x, n))) if (nrow(vec) > 0) nm <- c(nm[dfdetect], list(vec)) nm <- lapply(nm, as.data.frame) do.call(cbind, lapply(nm, function (df1) rbind(df1, as.data.frame(matrix(NA, ncol = ncol(df1), nrow = n-nrow(df1), dimnames = list(NULL, names(df1))))) )) } cbind.fill(data.frame(idx = numeric()), matrix(0, ncol = 2), data.frame(qwe = 1:3, rty = letters[1:3]), type = "GOOD", mark = "K-5") # idx V1 V2 qwe rty type mark # 1 NA 0 0 1 a GOOD K-5 # 2 NA NA NA 2 b GOOD K-5 # 3 NA NA NA 3 c GOOD K-5 

我只是发现一个技巧,当我们想要添加列到一个空的数据 ,只是在第一次,而不是后来的绑定

  newdf <- data.frame() # add the first column newdf <- rbind(newdf,data.frame("col1"=c("row1"=1,"row2"=2))) # add the second column newdf <- cbind(newdf,data.frame("col2"=c("row1"=3,"row2"=4))) # add more columns newdf <- cbind(newdf,data.frame("col3"=c("row1"=5,"row2"=6))) # result # col1 col2 col3 #row1 1 3 5 #row2 2 4 6 

我不知道为什么,但它对我有用。