Michal Píše
Michal Píše

Reputation: 13

How to add a new column with zero elements to an existing data frame?

I have a pre-existing frame with zero rows and I am trying to add a new column to it using the <- operator. However, R keeps ignoring me, probably because the added column also has zero elements. Is there any way to make R add the new column anyway?

frame <- data.frame('First' = I(c()))
frame[second.column.name] <- I(c())    # second.column.name is a variable, not actual column name

Upvotes: 1

Views: 3694

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81733

It works if you use [[ instead of [:

frame[["test"]] <- I(c())    
# [1] First test 
# <0 rows> (or 0-length row.names)

Upvotes: 1

Related Questions