How to include lines in a data.frame?

Asked

Viewed 8,030 times

5

Consider the following data.frame:

df <- data.frame(x=c("a","b"), y=c(1,2))

How to include a new line with x="c" and y=3 in data.frame?

3 answers

7


The function rbind make a copy of the data.frameand then add the line, which makes it inefficient.

An alternative is to use the function rbind_list package dplyr:

linha <- data.frame(x="c", y=3)
library(dplyr)
df <- rbind_list(df, linha)

In this case, as we are including a new level of factor x, it gives us a warning that is converting the variable to type character.

The benchmark is below:

library(microbenchmark)
time <- microbenchmark(
  rbind_list(df, linha),
  rbind(df, linha)
  )
time
## Unit: microseconds
##                   expr     min      lq   median       uq     max neval
##  rbind_list(df, linha)  85.342  90.478  95.2195 100.9485 234.295   100
##       rbind(df, linha) 258.396 269.855 279.1390 294.7460 528.250   100

5

One way to do this is to create a new data.frame with the line you want to include and use the function rbind:

linha <- data.frame(x="c", y=3)
df <- rbind(df, linha)
df
  x y
1 a 1
2 b 2
3 c 3

1

Another possibility is using the function add_row() of dplyr:

library(dplyr)

add_row(df, x="c", y=3)

  x y
1 a 1
2 b 2
3 c 3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.