Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cbind a dataframe with an empty dataframe - cbind.fill?

Tags:

dataframe

r

cbind

I think I'm looking for an analog of rbind.fill (in Hadley's plyr package) for cbind. I looked, but there is no cbind.fill.

What I want to do is the following:

#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 

Understandably, this produces an error:

Error in data.frame(..., check.names = FALSE) :  arguments imply differing number of rows: 0, 1 

My current fix is to replace the line return_df <- data.frame() with return_df <- data.frame(dummy=1) and then the code works. I then just remove dummy from the return_df at the end. After adding the dummy and running the above code, I get

      dummy a b        l  m 1     1 1 2 hi there 44 

I then just need to get rid of the dummy, e.g.:

> return_df[,2:ncol(return_df)]   a b        l  m 1 1 2 hi there 44 

I'm sure I'm missing an easier way to do this.

edit: I guess I'm not looking for a cbind.fill because that would mean that an NA value would be created after the cbind, which is not what I want.

like image 978
Xu Wang Avatar asked Nov 01 '11 03:11

Xu Wang


People also ask

What is the use of Cbind () function?

The cbind function is used to combine vectors, matrices and/or data frames by columns.

What is the use of Rbind () and Cbind () in R explain with example?

cbind() and rbind() both create matrices by combining several vectors of the same length. cbind() combines vectors as columns, while rbind() combines them as rows. Let's use these functions to create a matrix with the numbers 1 through 30.

Does Cbind create Dataframe?

The functions cbind and rbind are S3 generic, with methods for data frames. The data frame method will be used if at least one argument is a data frame and the rest are vectors or matrices.

How do I Cbind two data frames in R?

The cbind() and rbind() functions are generic methods for data frames. These data frame functions will be used if at least one argument is a data frame and the other are vectors or matrices. To merge two data frames (datasets) horizontally, use the merge() function in the R language.


2 Answers

Here's a cbind fill:

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)))))  } 

Let's try it:

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,]) 

I think I stole this from somewhere.

EDIT STOLE FROM HERE: LINK

like image 136
Tyler Rinker Avatar answered Oct 18 '22 08:10

Tyler Rinker


While, I think Tyler's solution is direct and the best here, I just provide the other way, using rbind.fill() that we already have.

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))))                                                                                                                           }  
like image 29
Max Avatar answered Oct 18 '22 08:10

Max