Create a new row in r
In R, you can create a new row in a data frame using the rbind()
function. Here's an example:
Suppose you have a data frame called df
with the following structure:
> df
name age
1 John 25
2 Jane 30
3 Bob 35
To add a new row to df
, you can use the rbind()
function like this:
new_row <- data.frame(name = "Alice", age = 28)
df <- rbind(df, new_row)
This will add a new row to df
with the values "Alice" and 28:
> df
name age
1 John 25
2 Jane 30
3 Bob 35
4 Alice 28
Note that rbind()
returns a new data frame that is the result of combining the original data frame with the new row. The original data frame is not modified.
Alternatively, you can use the add_row()
function from the dplyr
package:
library(dplyr)
df <- add_row(df, name = "Alice", age = 28)
This will also add a new row to df
with the same values.