Following the post about %in% operator, I received this tweet:
I gave a look to the code kindly provided by Ben and then I asked myself:
I know dplyr is a really nice package, but which snippet is faster?
to answer the question I’ve put the two snippets in two functions:
filter(object,object[,column] %in% vector)
object[object[,column] %in% vector,]
Then, thanks to the great package microbenchmark, I made a comparison between those two functions, testing the time of execution of both, for 100.000 times.
comparison = microbenchmark(Rbase_snippet(iris,5,vec),dplyr_snippet(iris,5,vec),times = 100000)
#plot the output
labs(title = "comparison between dplyr_snippet and Rbase_snippet", y="snippet")
And that was the result:
R Base package seems to be the winner, even if just for an handful of microseconds…
Nevertheless, I am really grateful to Ben, it was a great fun!
If you have to put some math writing into your Evernote notes, and you have a Mac device, there is a very simple way to solve your problem out.
This way is called Grapher, a built-in application for visualising math stuffs.
Here below a simple tutorial:
1. find Grapher among your applications. You can either search for it within Spotlight or using the launchpad.
2. write the equation you would like to put into your Evernote note.
3. copy the equation
4. paste the equation into Evernote
And that’s it!
I think this trick is very useful when you have some “heavy” equations that would not be clear enough if it would just be written in simple text.
I think the Grapher advantage is that is a built-in application, nevertheless I’m really grateful to Roberta Normano
for the tip.
Other tips are welcome!
you have to subset a data frame using as criteria the exact match of a vector content.
you have a dataset with some attributes, and you have a vector with some values of one of the attributes. You want to make a filter based on the values in the vector.
Example: sales records, each record is a deal.
The vector is a list of selected customers you are interested in.
Is it possible to make such a kind of filter?
Of course it is!
you just have to use the %in% operator.
let’s see how to do it in the short tutorial here below.
suppose you have a sales data frame object like this:
suppose you want to extract sales to Francesca, Tommaso and Gianna.
first, you have to assign those names to a vector.
vector = c(“Francesca","Tommaso","Gianna")
then, you can write the filtering statement, using the %in% operator.
query_result = sales[sales$customer %in% vector,]
and that’s it!
The meaning of %in% operator is exactly the one you guess:
“ select only values present IN the specified group”.
Full code is available as an R workbook for quick reference:
Let me know if you use any other method to obtain the same result.
Finally, if you enjoyed the tutorial, you can find more tutorial on page Tutorial (quite obvious, isnt’ it?).