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
?
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
?
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
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