R365: Day 25: attach() and detach()

I have been going through various packages and analyses and sometimes its good to just step back and ask ‘’why in the world is this not working?”. I explored the different datasets that are available in R in the {datasets} package, and we saw several other datasets strewn about in various stages of usefulness (KMsurv from my most recent post comes to mind), but sometimes you want to work with just one single dataset and you don’t want to deal with specifying the column type using the ‘’$’’ symbol every time. This is useful if you just want to hold on to a particular portion of a dataset as a vector.

library(survival)
 library(KMsurv)
 data(aids)
 attach(aids)
 plot(infect~induct)

Rplot1

Now you can call different columns from the {aids} dataset, like the infection or induction time for AIDS. This is pretty cool, but one of the cool (and depressing) things about this dataset is that they have split it up by whether the patients were adults or not. Maybe we can separate out the children and adult datasets to see what kind of differences there are. After searching for a bit, I gave up on doing this with the base package, and decided to do it using {ggplot2} using the help from this post.

library(ggplot2)
 #GGPLOT
 qplot(x=infect, y=induct,
 + data=aids,
 + colour=adult,
 + main="AIDS infect~induction") +
 + geom_point()+xlab("induction")+ylab("infection")

Rplot01

Which is pretty nice! Much nicer than I thought it would be, I was honestly getting kind of worried. After you’re all done, remember to detach() your data, otherwise R will get confused if you work with another package with similarly titled columns.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s