Using Reshape2 for transposing datasets in R

Note Problem Statement-This is quite similar to using Proc Transpose using values in SAS. see http://analytics.ncsu.edu/sesug/2005/TU12_05.PDF
Diagram using geneplotter from R Graph Gallery
In R however this can be done as follow.
convert dataframe
Subject     Item      Score
1 Subject 1     Item 1     1
2 Subject 1     Item 2     0
3 Subject 1     Item 3     1
4 Subject 2     Item 1     1
5 Subject 2     Item 2     1
6 Subject 2      Item 3     0
to
Subject     Item 1 Item 2 Item 3 Item 4
1 Subject 1      1              0               1                 1
5 Subject 2      1              1                0                0
Note- I am using http://www.inside-r.org/pretty-r/tool for auto-generating the color coded R Code.
library("reshape2")
tDat.m<- melt(tDat)tDatCast<- acast(tDat.m,Subject~Item)
and that's it!
Another way (this one is  not recommended as it seems to take longer
 and more memory)
df.wide <- reshape(df, idvar="Subject", timevar="Item", direction="wide")

For R Writers- Inside R

A composite of the GNU logo and the OSI logo, ...
Image via Wikipedia

Hurray I am on Inside -R

http://www.inside-r.org/blogs/2010/11/04/r-apache-next-frontier-r-computing

Thats blog post number 1 there.

Basically Inside R is a go-to site for tips, tricks, packages, as well as blog posts. It thus enhances R Bloggers – but also adds in other multiple features as well.

It is an excellent place for R beginners and learning R. Also it is moderated ( so you wont get the flashy jhing bhang stuff- just your R.

What I really liked is the Pretty R functionality for turning R code -its nifty for color coding R code for use of posting in your blog, journal or article

and when you are there drop them a line for their excellent R support for events (like Pizza, sponsorship) and nifty R packages (doSNOW, foreach, RevoScaler, RevoDeployR) and how much open core makes them look silly?

Come on Revolution- share the open code for RevoScaler package- did you notice any sales dip when you open sourced the other packages? (cue to David Smith to roll his eyes again)

Anyway- all that is part of the R family fun 🙂

Do check http://www.inside-r.org/pretty-r

 

Hey Americano- outsource your job here

Mystiq (Raphael-Raven Darkholme). Art by Tom G...
Image via Wikipedia

Yup Yankee Doodle. Welcome to India. Ancient Land of Mystique. Modern Land of taking your job.

Let me give you how many jobs we created in India from you.

“Generation of employment

The rapid growth in IT-BPO industry has created large number of jobs for the expanding employablepopulation. The employment provided by the industry increased more than 8 times over FY2000-2009and reached 2.2 million in FY2009.”

Thats stratight from the horses mouth  from http://www.nasscom.in/upload/68924/Impact_Study_2010_Exec_Summary.pdf

2.2 million jobs. Assume 50% of jobs catered to US. Thats 1.1 Million . Assume lower productivity by Indians of 10%.

That’s a million jobs, Americano. Thanks for nothing.

Thats 1 million people who went from taking a paycheck and spending it (on Chinese manufactured good) to 1 million welfare checks.

Comprende?

Now I wish we could drink more Coke, buy more Levis, or even more Fighter Aircraft using our 250 $ billion reserves, but we follow China the leader.

See

http://www.stats.gov.cn/english/newsandcomingevents/t20101021_402677638.htm

6. Foreign trade grew rapidly and trade surplus was reduced to some extent. The total value of imports and exports for the first three quarters of this year was US$ 2,148.7 billion, up by 37.9 percent year-on-year. The value of exports was US$ 1,134.6 billion, up by 34.0 percent, and the value of imports was US$ 1,014.0 billion, up by 42.4 percent. The trade surplus was US$ 120.6 billion, a decline of US$ 14.9 billion over the same period last year.”

Thats a lot of money employing a lot of Chinese, maybe even more than the 1 million American jobs that went to India.

From http://www.epi.org/publications/entry/bp260/

The computers, electronic equipment, and parts industries experienced the largest growth in trade deficits with China, leading with 627,700 (26%) of all jobs displaced between 2001 and 2008. As a result, the hardest hit Congressional districts were located in California and Texas, where remaining jobs in those industries are concentrated, and in North Carolina, which was hard hit by job displacement in a variety of manufacturing industries

NOTE- Thats also a very sensitive area in terms of security cyber et all

Anyways, thats all now. Thanks for the jobs Yankee boys

(written by an Indian who dislikes job losses anywhere)

 

Google Maps – Jet Ski across Pacific Ocean

Don’t belive it huh

Go to http://goo.gl/maps/UISY

Instruction 43: (From Japan to China) is Jet Ski for 782 km

Very funny- and maybe an easter egg

R Apache – The next frontier of R Computing

I am currently playing/ trying out RApache- one more excellent R product from Vanderbilt’s excellent Dept of Biostatistics and it’s prodigious coder Jeff Horner.

The big ninja himself

I really liked the virtual machine idea- you can download a virtual image of Rapache and play with it- .vmx is easy to create and great to share-

http://rapache.net/vm.html

Basically using R Apache (with an EC2 on backend) can help you create customized dashboards, BI apps, etc all using R’s graphical and statistical capabilities.

What’s R Apache?

As  per

http://biostat.mc.vanderbilt.edu/wiki/Main/RapacheWebServicesReport

Rapache embeds the R interpreter inside the Apache 2 web server. By doing this, Rapache realizes the full potential of R and its facilities over the web. R programmers configure appache by mapping Universal Resource Locaters (URL’s) to either R scripts or R functions. The R code relies on CGI variables to read a client request and R’s input/output facilities to write the response.

One advantage to Rapache’s architecture is robust multi-process management by Apache. In contrast to Rserve and RSOAP, Rapache is a pre-fork server utilizing HTTP as the communications protocol. Another advantage is a clear separation, a loose coupling, of R code from client code. With Rserve and RSOAP, the client must send data and R commands to be executed on the server. With Rapache the only client requirements are the ability to communicate via HTTP. Additionally, Rapache gains significant authentication, authorization, and encryption mechanism by virtue of being embedded in Apache.

Existing Demos of Architechture based on R Apache-

  1. http://rweb.stat.ucla.edu/ggplot2/ An interactive web dashboard for plotting graphics based on csv or Google Spreadsheet Data
  2. http://labs.dataspora.com/gameday/ A demo visualization of a web based dashboard system of baseball pitches by pitcher by player 

 

 

 

 

 

 

 

3. http://data.vanderbilt.edu/rapache/bbplot For baseball results – a demo of a query based web dashboard system- very good BI feel.

Whats coming next in R Apache?

You can  download version 1.1.10 of rApache now. There
are only two significant changes and you don’t have to edit your
apache config or change any code (just recompile rApache and
reinstall):

1) Error reporting should be more informative. both when you
accidentally introduce errors in the Apache config, and when your code
introduces warnings and errors from web requests.

I’ve struggled with this one for awhile, not really knowing what
strategy would be best. Basically, rApache hooks into the R I/O layer
at such a low level that it’s hard to capture all warnings and errors
as they occur and introduce them to the user in a sane manner. In
prior releases, when ROutputErrors was in effect (either the apache
directive or the R function) one would typically see a bunch of grey
boxes with a red outline with a title of RApache Warning/Error!!!.
Unfortunately those grey boxes could contain empty lines, one line of
error, or a few that relate to the lines in previously displayed
boxes. Really a big uninformative mess.

The new approach is to print just one warning box with the title
“”Oops!!! <b>rApache</b> has something to tell you. View source and
read the HTML comments at the end.” and then as the title implies you
can read the HTML comment located at the end of the file… after the
closing html. That way, you’re actually reading how R would present
the warnings and errors to you as if you executed the code at the R
command prompt. And if you don’t use ROutputErrors, the warning/error
messages are printed in the Apache log file, just as they were before,
but nicer 😉

2) Code dispatching has changed so please let me know if I’ve
introduced any strange behavior.

This was necessary to enhance error reporting. Prior to this release,
rApache would use R’s C API exclusively to build up the call to your
code that is then passed to R’s evaluation engine. The advantage to
this approach is that it’s much more efficient as there is no parsing
involved, however all information about parse errors, files which
produced errors, etc. were lost. The new approach uses R’s built-in
parse function to build up the call and then passes it of to R. A
slight overhead, but it should be negligible. So, if you feel that
this approach is too slow OR I’ve introduced bugs or strange behavior,
please let me know.

FUTURE PLANS

I’m gaining more experience building Debian/Ubuntu packages each day,
so hopefully by some time in 2011 you can rely on binary releases for
these distributions and not install rApache from source! Fingers
crossed!

Development on the rApache 1.1 branch will be winding down (save bug
fix releases) as I transition to the 1.2 branch. This will involve
taking out a small chunk of code that defines the rApache development
environment (all the CGI variables and the functions such as
setHeader, setCookie, etc) and placing it in its own R package…
unnamed as of yet. This is to facilitate my development of the ralite
R package, a small single user cross-platform web server.

The goal for ralite is to speed up development of R web applications,
take out a bit of friction in the development process by not having to
run the full rApache server. Plus it would allow users to develop in
the rApache enronment while on windows and later deploy on more
capable server environments. The secondary goal for ralite is it’s use
in other web server environments (nginx and IIS come to mind) as a
persistent per-client process.

And finally, wiki.rapache.net will be the new www.rapache.net once I
translate the manual over… any day now.

From –http://biostat.mc.vanderbilt.edu/wiki/Main/JeffreyHorner

 

 

Not convinced ?- try the demos above.