Adding to a zero-row data.frame
will act differently to adding to an data.frame
that already contains rows
From ?rbind
The rbind data frame method first drops all zero-column and zero-row arguments. (If that leaves none, it returns the first argument with columns otherwise a zero-column zero-row data frame.) It then takes the classes of the columns from the first data frame, and matches columns by name (rather than by position). Factors have their levels expanded as necessary (in the order of the levels of the levelsets of the factors encountered) and the result is an ordered factor if and only if all the components were ordered factors. (The last point differs from S-PLUS.) Old-style categories (integer vectors with levels) are promoted to factors.
You have a number of options --
the most straightforward
compData[1, ] <- c(5, 443)
more complicated
Or you could coerce c(5,433)
to a list or data.frame
rbind(compData,setNames(as.list(c(5,443)), names(compData)))
or
rbind(compData,do.call(data.frame,setNames(as.list(c(5,443)), names(compData))))
But in this case you might as well do
do.call(data.frame,setNames(as.list(c(5,443)), names(compData)))
data.table option
You could use the data.table
function rbindlist
which does less checking and thus preserves the names of the first data.frame
library(data.table)
rbindlist(list(compData, as.list(c(5,443))