Possible Duplicate:
R: losing column names when adding rows to an empty data frame
I created an empty dataframe with column names only as follows
> compData <- data.frame(A= numeric(0), B= numeric(0))
> compData
[1] A B
<0 rows> (or 0-length row.names)
> compData <- rbind(compData,c(5,443))
> compData
X5 X443
1 5 443
in the above after adding one row the column names are changed. How can I add new row data to data-frame?
Adding to a zero-row
data.framewill act differently to adding to andata.framethat already contains rowsFrom
?rbindYou have a number of options —
the most straightforward
more complicated
Or you could coerce
c(5,433)to a list or data.frameor
But in this case you might as well do
data.table option
You could use the
data.tablefunctionrbindlistwhich does less checking and thus preserves the names of the first data.frame