[R] need help reshaping table using aggregate

Tim tfox at mde.state.md.us
Wed Jun 20 20:58:06 CEST 2012


I am trying to learn how to reshape my data set.  I am new to R, so please
bear with me.  Basically, I have the following data set:

site<-c("A","A","B","B")
bug<-c("spider","grasshopper","ladybug","stinkbug")
count<-c(2,4,6,8)
myf <- data.frame(site, bug, count)
myf

  site         bug count
1    A      spider     2
2    A grasshopper     4
3    B     ladybug     6
4    B    stinkbug     8

This means that in site A, I found 2 spiders and 4 grasshopper.  In site B,
I found 6 ladybugs and 8 stinkbugs.

I would like to change the df to aggregate the site column and make the bugs
columns so it arranged like this:

  site spider grasshopper ladybug stinkbug
1    A      2           4       0        0
2    B      0           0       6        8


--
View this message in context: http://r.789695.n4.nabble.com/need-help-reshaping-table-using-aggregate-tp4634014.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list