How can a add a row to a data frame in R?
Like @Khashaa and @Richard Scriven point out in comments, you have to set consistent column names for all the data frames you want to append.
Hence, you need to explicitly declare the columns names for the second data frame, de
, then use rbind()
. You only set column names for the first data frame, df
:
df<-data.frame("hi","bye")
names(df)<-c("hello","goodbye")
de<-data.frame("hola","ciao")
names(de)<-c("hello","goodbye")
newdf <- rbind(df, de)
Let's make it simple:
df[nrow(df) + 1,] = c("v1","v2")
Or, as inspired by @MatheusAraujo:
df[nrow(df) + 1,] = list("v1","v2")
This would allow for mixed data types.