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.
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.
>
Tim - Another approach to your problem is to use xtabs:> xtabs(count~site+bug,data=myf)bug site grasshopper ladybug spider stinkbug A 4 0 2 0 B 0 6 0 8 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Wed, 20 Jun 2012, Tim wrote:> 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. >