Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Tuesday, February 5, 2013

Doing median and means in R

Let's say you have a dataframe d2 with fields industry, employee_range and multiplier. If you want to find the median of the field "multiplier" over a specific industry and employee_range you can use the subset function and specify the filter criterion


> median(subset(d2,industry=="Software & Technology" & employee_range=="Enterprise")$multiplier)
[1] 244659.3



Thursday, December 13, 2012

How to create histogram in R using single column data

Lets say you have a data file file.txt with all the values you need the histogram to be generated on.

file.txt
1
2
3
1
1
0


In R, you can simply load the file in a vector and not a data frame using scan
x <- br="br" file.txt="file.txt" scan="scan">

Then run the following
hist(x,breaks=5)

Where breaks is the number of bins you want the histogram to span.


Thursday, July 5, 2012

Using R as a Reporting Engine for MySQL using ggplot2 and knitr

R has a package called ggplot2 which can be used with knitr to generate HTML reports from a MySQL database.

You'll require the knitr, RMySQL, markdown packages. You can get them by typing the following in R Studio

install.packages("knitr")
install.packages("RMySQL")
install.packages("markdown")

The HTML will be created by the knit2html command in the knitr package. knit2html converts a R Markdown file to HTML. R Markdown is extension of markdown that allows you to write embedded R code in your markdown file. Consider this sample R markdown file DataReport.Rmd that uses RMySQL to get report data from a MySQL database, stores it in a R data frame, uses ggplot2 to plot the graph and finally converts to HTML.

DataReport.Rmd
 ![alt text](http://i279.photobucket.com/albums/kk147/SOCTONE/R_logo.jpg)  
 Title  
 ========================================================  
 ### Top 10 Countries  
 ```{r echo=FALSE, include=FALSE}  
 library(RMySQL)  
 library(ggplot2)  
 con2 <- dbConnect(MySQL(), user="user", password="pwd",dbname="schemaname", host="host")  
 counts_query <- dbSendQuery(con2, "select country, count(*) count from table group by country limit 10")  
 counts_data = fetch(counts_query,-1)  
 ```  
 ```{r echo=FALSE}  
 ggplot(counts_data, aes(country, count, fill=country)) + geom_bar() + coord_flip() + opts(legend.position="none")  
 ```  

To generate the HTML from the R Studio console run
 knit2html("DataReport.Rmd")  


You can drive the whole process of a shell script so that reports can be run on demand or periodically via crontab. See my post on how to drive R scripts off shell. You can later copy the resulting html file to a webserver so that it can be hosted and shared on the web/intranet.

Thursday, June 28, 2012

Automate Visualizations Using R and ggplot2

R is a pretty nifty tool that allows you to do tons of statistical analysis on your data. ggplot2 is a package inside R developed by Hadley Wickham that allows you to create visualizations. I had the honor to see Hadley today at a meetup in San Francisco. Hadley was going over the basics of ggplot2 and it struck me - "wouldn't it be cool to automate visualization creation so you can create visualizations without ever opening up R". I did some research and found it is possible.

Basically, you can use R scripts to automate your work. Then call the script on command line. Here's the code to store the graph in a png file using ggplot2. You need to have R installed for this. I'm using the standard dataset "diamonds" that came with ggplot2.

R Console can be called from the command line by simply typing r. To quit out of the console type q().



Now create a script file test.r with the following

library(ggplot2)
mygraph <- qplot(price, ..density.. ,data = diamonds, binwidth = 250,geom="freqpoly")
ggsave(mygraph,file="/Users/yasha/Desktop/delme.png")

After creating the script file run the file using R CMD BATCH on the command line.

R CMD BATCH /Users/yasha/Desktop/test.r 

This creates delme.png on the Desktop. 

You can later embed this file in a document to create a report, email it or whatever. Neat stuff! This process allows you to call R externally. You can probably automate the creation of script using Ruby or Shell or Java or whatever you like to use. Then run the script and enjoy the sweet visualizations.

Hadley Wickham has a great set of slides for introductory ggplot2. Check it out at https://dl.dropbox.com/u/41902/ggplot2-intro.pdf