Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple columns to a data.frame in one go?

Tags:

dataframe

r

I have following dataframe and vector:

ddf = data.frame(a=rep(1,10), b=rep(2,10)) xx = c("c", "d", "e", "f") 

How can I new empty columns which are named with items in xx ?

I tried following but it does not work:

ddf = cbind(ddf, data.frame(xx)) Error in data.frame(..., check.names = FALSE) :    arguments imply differing number of rows: 10, 4 

Following also does not work:

for(i in 1:length(xx)){     ddf$(xx[i]) = ""   }  Error: unexpected '(' in: "for(i in 1:length(xx)){ ddf$("  } Error: unexpected '}' in "}" 
like image 501
rnso Avatar asked Aug 18 '14 06:08

rnso


People also ask

How do you add multiple data frames in one data frame?

The concat() function can be used to concatenate two Dataframes by adding the rows of one to the other. The merge() function is equivalent to the SQL JOIN clause. 'left', 'right' and 'inner' joins are all possible.

How do I add multiple columns to an empty data frame?

There are multiple ways to add a new empty/blank column (single or multiple columns) to a pandas DataFrame by using assign operator, assign() , insert() and apply() methods. By using these you can add one or multiple empty columns with either NaN , None , Blank or Empty string values to all cells.


1 Answers

This will get you there:

ddf[xx] <- NA  #   a b  c  d  e  f #1  1 2 NA NA NA NA #2  1 2 NA NA NA NA #3  1 2 NA NA NA NA #... 

You can't directly use something like ddf$xx because this will try to assign to a column called xx rather than interpreting xx. You need to use [ and [<- functions, using the square brackets when you are dealing with a character string/vector - like ddf["columnname"] or ddf[c("col1","col2")], or a stored vector like your ddf[xx].

The reason why it selects columns is because data.frames are lists essentially:

is.list(ddf) #[1] TRUE  as.list(ddf) #$a # [1] 1 1 1 1 1 1 1 1 1 1 #  #$b # [1] 2 2 2 2 2 2 2 2 2 2 

...with each column corresponding to a list entry. So if you don't use a comma to specify a row, like ddf["name",] or a column like ddf[,"name"], you get the column by default.


In the case that you are working with a 0-row dataset, you can not use a value like NA as the replacement. Instead, replace with list(character(0)) where character(0) can be substituted for numeric(0), integer(0), logical(0) etc, depending on the class you want for your new columns.

ddf <- data.frame(a=character()) xx <- c("c", "d", "e", "f") ddf[xx] <- list(character(0)) ddf #[1] a c d e f #<0 rows> (or 0-length row.names) 
like image 139
thelatemail Avatar answered Oct 10 '22 21:10

thelatemail