Answering to Ben ( functions comparison in R)

Answering to Ben ( functions comparison in R)

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:

#Ben snippet
dplyr_snippet =function(object,column,vector){
filter(object,object[,column] %in% vector)
}
#AC snippet
Rbase_snippet =function(object,column,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
autoplot(comparison)+
labs(title = "comparison between dplyr_snippet and Rbase_snippet", y="snippet")

And that was the result:

snippet_comparison

 

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!

How to Put Equations into Evernote

How to Put Equations into Evernote
Problem
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.
Solution
This way is called Grapher, a built-in application for visualising math stuffs.
Tutorial
Here below a simple tutorial:
1. find Grapher among your applications. You can either search for it within Spotlight or using the launchpad.
1equations in evernote
2. write the equation you would like to put into your Evernote note.

2 equations in evernote

 

3. copy the equation as TIFF
 equations in evernote
4. paste the equation into Evernote

3 equations in 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.
Sharing the post on Google + I have received the good advice to use daum equation editor, specifically aimed at writing equations.
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!

Code snippet: subsetting data frame in R by vector

Code snippet: subsetting data frame in R by vector

Problem:

you have to subset a data frame using as criteria the exact match of a vector content.

for instance:

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?

Solution

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.

Tutorial

suppose you have a sales data frame object like this:

#rstats

 

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:

filter_code.R

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