Using grepl in R -
in cases these 2 different ways of implimentation give different results?
data(mtcars) firstway <- mtcars[grepl('6',mtcars$cyl),] secondway <- mtcars[mtcars$cyl=='6',]
if these ways give same results of them recommended , why? thanks
using package microbenchmark, can see faster
library(microbenchmark) m <- microbenchmark(mtcars[grepl('6',mtcars$cyl),], mtcars[mtcars$cyl=='6',], times=10000) unit: microseconds expr min lq mean median uq max neval mtcars[grepl("6", mtcars$cyl), ] 229.080 234.738 247.5324 236.693 239.417 6713.914 10000 mtcars[mtcars$cyl == "6", ] 214.902 220.210 231.0240 221.956 224.471 7759.507 10000
it looks ==
faster, when possible should use that
however, functions not same thing. grepl
searches if string present @ wheras ==
checks whether expressions equal
grepl("6", mtcars$disp) [1] true true false false true false true true false true true false false false false true false [18] false false false false false false false false false false false false false false false mtcars$disp == "6" [1] false false false false false false false false false false false false false false false false false [18] false false false false false false false false false false false false false false false
Comments
Post a Comment