In which cases could these 2 different ways of implimentation would give different results?
data(mtcars)
firstWay <- mtcars[grepl('6',mtcars$cyl),]
SecondWay <- mtcars[mtcars$cyl=='6',]
If these ways always give the same results which of them is recommended and why? Thanks
Using the package microbenchmark, we can see which is faster
It looks like
==
is faster, so when possible you should use thatHowever, the functions do not do exactly the same thing.
grepl
searches for if the string is present at all wheras==
checks whether the expressions are equal