[R] need help reshaping table using aggregate
Rui Barradas
ruipbarradas at sapo.pt
Wed Jun 20 21:56:39 CEST 2012
Hello,
Try
library(reshape2)
mlt <- melt(myf, id=c("site", "bug"), measure="count")
myf2 <- dcast(mm, site ~ bug)
myf2[is.na(myf2)] <- 0
myf2
Hope this helps,
Rui Barradas
Em 20-06-2012 19:58, Tim escreveu:
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list