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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With