Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How can a add a row to a data frame in R?

In R, how do you add a new row to a data frame once the data frame has already been initialized?

So far I have this:
df <- data.frame("hi", "bye")
names(df) <- c("hello", "goodbye")

#I am trying to add "hola" and "ciao" as a new row
de <- data.frame("hola", "ciao")

merge(df, de) # Adds to the same row as new columns

# Unfortunately, I couldn't find an rbind() solution that wouldn't give me an error


Any help would be appreciated
by

2 Answers

vishaljlf39
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)
RoliMishra
Let's make it simple:

df[nrow(df) + 1,] = c("v1","v2")

Login / Signup to Answer the Question.