How to include rows in a data.frame?

3

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 ?

    
asked by anonymous 14.09.2014 / 14:47

2 answers

4

The rbind function makes a copy of the data.frame and then adds the line, which makes it inefficient.

An alternative is to use the rbind_list function of the dplyr package:

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

In this case, since we are including a new level of the x factor, 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
    
15.09.2014 / 13:28
3

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

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