user1356855
user1356855

Reputation: 585

Add a column to empty data.frame

I want to initialise a column in a data.frame look so:

df$newCol = 1

where df is a data.frame that I have defined earlier and already done some processing on. As long as nrow(df)>0, this isn't a problem, but sometimes my data.frame has row length 0 and I get:

> df$newCol  = 1
Error in `[[<-`(`*tmp*`, name, value = 1) :
 1 elements in value to replace 0 elements

I can work around this by changing my original line to

df$newCol = rep(1,nrow(df))

but this seems a bit clumsy and is computationally prohibitive if the number of rows in df is large. Is there a built in or standard solution to this problem? Or should I use some custom function like so

addCol = function(df,name,value) {
  if(nrow(df)==0){
    df[,name] = rep(value,0)
  }else{
    df[,name] = value
  }
  df
}

Upvotes: 2

Views: 8482

Answers (1)

jakub
jakub

Reputation: 5104

If I understand correctly,

df = mtcars[0, ]
df$newCol = numeric(nrow(df))

should be it?

This is assuming that by "row length" you mean nrows, in which case you need to append a vector of length 0. In such case, numeric(nrow(df)) will give you the exact same result as rep(0, nrow(df)).

It also kind of assumes that you just need a new column, and not specifically column of ones - then you would simply do +1, which is a vectorized operation and therefore fast.

Other than that, I'm not sure you can have an "empty" column - the vector should have the same number of elements as the other vectors in the data frame. But numeric is fast, it should not hurt.

Upvotes: 7

Related Questions