mt1022
mt1022

Reputation: 17289

How to add multiple empty columns to a data.frame with 0 rows?

I can add columns to data.frame:

x <- head(iris)
y <- x[1:nrow(x) > 7, ]
x[c('NewCol1', 'NewCol2')] <- rep(c('a', 'b'), each = nrow(x))

For a data.frame with 0 row, it does not work:

# > y
# [1] Sepal.Length Sepal.Width  Petal.Length Petal.Width  Species     
# <0 rows> (or 0-length row.names)

y[c('NewCol1', 'NewCol2')] <- rep(c('a', 'b'), each = nrow(y))

# Error in value[[jvseq[[jjj]]]] : subscript out of bounds

I found this, Add Columns to an empty data frame in R, but it doesn't help much.

expected output:

# > y
# [1] Sepal.Length Sepal.Width  Petal.Length Petal.Width  Species      NewCol1      NewCol2
# <0 rows> (or 0-length row.names)

Upvotes: 3

Views: 7354

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388817

We can use read.table by setting col.names parameter

read.table(text = "",col.names =  c(names(y), c("New_Col1", "New_Col2")))

#Sepal.Length Sepal.Width  Petal.Length Petal.Width  Specie   New_Col1  New_Col2   
#<0 rows> (or 0-length row.names)

We can also set our desired class by using the colClasses parameter

read.table(text = "",col.names =  c(names(y), c("New_Col1", "New_Col2")), 
                colClasses = c(sapply(y, class), "character", "character"))

So in this case the two new variables will get character class.

Upvotes: 3

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520908

Consider the following code which creates an empty data frame:

df <- data.frame(Ints=integer(),
                 Characters=character(),
                 stringsAsFactors=FALSE)

One way to add a new column to this empty data frame is to use cbind():

df2 <- cbind(df, data.frame(Stuff=character(),stringsAsFactors=FALSE))

> df2
[1] Ints       Characters Stuff     
<0 rows> (or 0-length row.names)

Then just add your data as you normally would, e.g.

> df2[1,] <- c(1, "hello", "world")
> df2
  Ints Characters Stuff
1    1      hello world

As you mentioned, this might cause a casting problem in the Ints column. Assigning each column by itself avoids this, e.g.

df2$Ints <- c(1:5)
df2$Stuff <- c("one", "two", "three", "four", "five")

Or, you could use something like read.table to bring in your data, and explicitly assign the classes that way.

Upvotes: 4

Related Questions