[R] summing vectors

arun smartpink111 at yahoo.com
Thu Apr 4 18:00:23 CEST 2013


HI Nicolas,

Your dput() looks like corrupted.
#changed
dat2<- structure(list(Year = c(2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2001L), Area = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), Q = c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L), Bin = c(5L, 
10L, 15L, 20L, 5L, 10L, 15L, 20L, 5L, 10L, 15L, 20L, 5L, 10L), 
    FD = c(0L, 1L, 23L, 12L, 4L, 1L, 3L, 15L, 4L, 8L, 12L, 23L, 
    12L, 8L)), .Names = c("Year", "Area", "Q", "Bin", "FD"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14"))

library(reshape2)
res<-dcast(dat2,Bin~Area+Year,value.var="FD",sum)
 names(res)[grep("^\\d+",names(res))]<-paste0("FD_",names(res)[grep("^\\d+",names(res))])
 res
#  Bin FD_1_2000 FD_2_2000 FD_2_2001
#1   5         4        16         0
#2  10         2         8         8
#3  15        26        12         0
#4  20        27        23         0

I am not sure 8+8.., 12+.. 23+.. .  I hope that is from the bigger dataset.
A.K.




________________________________
 From: Nicolas L. Gutierrez <nicolasg at uw.edu>
To: arun <smartpink111 at yahoo.com> 
Sent: Thursday, April 4, 2013 10:49 AM
Subject: Re: [R] summing vectors
 

Hi Arun, 
Thanks for your fast response.. I have included what I need following your mock dataset:
 

dat2<- structure(list(Year = c(2000L, 2000L, 2000L, 2000L, 2000L, 2000L,
 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2001L), Area = c(1L, 1L, 1L, 1L, 1L, 1L,1L , 1L, 2L, 2L, 2L, 2L, 2L,2L), Q = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,2L), Bin = c(5L,
 10L, 15L, 20L, 5L, 10L, 15L, 20L, 5L, 10L, 15L, 20L, 5L, 10L), FD = c(0L, 1L, 23L, 12L,
 4L, 1L, 3L, 15L, 4L,8L, 12L, 23L, 12L, 8L, 1L)), .Names = c("Year", "Area", "Q", "Bin",
 "FD"), class = "data.frame", row.names = c(NA, -14L))

I need:
 
Bin   FD_2000_1   FD_2000_2     FD_2000_3 ... FD_2012_3
5         4                    4+12
10       2                    8+8
15      26                  12+...
20      27                  23+...
 
Basically the column FD_2000_1groups FD by Bin  for year 2000 and Area 1. I have 12 years in my dataset, about 20 areas. Sorry if I'm being confusing.
 
Cheers,
NG 



On Thu, Apr 4, 2013 at 3:20 PM, arun <smartpink111 at yahoo.com> wrote:


>
>
>You could also use:
>library(data.table)
>dat2<- data.table(dat1)
>dat2<-dat2[,setdiff(colnames(dat2),"Q"),with=FALSE]
> dat2[,lapply(.SD,sum),by=c("Year","Area")]
>
>#   Year Area Bin FD
>#1: 2000    1  50 36
>#2: 2001    1   1  4
>#3: 2001    2  50 23
>A.K.
>
>
>----- Original Message -----
>
>From: arun <smartpink111 at yahoo.com>
>To: Nicolas L. Gutierrez <nicolasg at uw.edu>
>Cc: R help <r-help at r-project.org>
>Sent: Thursday, April 4, 2013 10:08 AM
>Subject: Re: [R] summing vectors
>
>HI,
>
>I assume that you have a data.frame structure.
>dat1<- structure(list(Year = c(2000L, 2000L, 2000L, 2000L, 2001L, 2001L,
>2001L, 2001L, 2001L), Area = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
>2L), Q = c(1L, 1L, 1L, 1L, 25L, 1L, 1L, 1L, 1L), Bin = c(5L,
>10L, 15L, 20L, 1L, 5L, 10L, 15L, 20L), FD = c(0L, 1L, 23L, 12L,
>4L, 1L, 3L, 15L, 4L)), .Names = c("Year", "Area", "Q", "Bin",
>"FD"), class = "data.frame", row.names = c(NA, -9L))
>
> aggregate(.~Year+Area,data=dat1[,-3],sum)
>#  Year Area Bin FD
>#1 2000    1  50 36
>#2 2001    1   1  4
>#3 2001    2  50 23
>
>#or
>library(plyr)
>ddply(dat1,.(Year,Area),colwise(sum,c("Bin","FD")))
>#  Year Area Bin FD
>#1 2000    1  50 36
>#2 2001    1   1  4
>#3 2001    2  50 23
>A.K.
>
>
>
>----- Original Message -----
>From: Nicolas L. Gutierrez <nicolasg at uw.edu>
>To: r-help at r-project.org
>Cc:
>Sent: Thursday, April 4, 2013 8:42 AM
>Subject: [R] summing vectors
>
>Hi All,
>Year Area Q Bin FD
>       I have a large dataset I need to re-structure. It looks something
>like this: 2000 1 1 5 0  2000 1 1 10 1  2000 1 1 15 23  2000 1 1 20 12  2000
>1 1 25 1  2000 2 1 5 1  2000 2 1 10 3  2000 2 1 15 15  2000 2 1 20 11  2000
>2 1 25 3  2000 1 2 5 0  2000 1 2 10 1  2000 1 2 15 23  2000 1 2 20 12  2000
>1 2 25 1  2000 2 2 5 1  2000 2 2 10 3  2000 2 2 15 15  2000 2 2 20 11  2000
>2 2 25 3  2001    …    2011
>
>
>
>
>  And I need to create different vectors (Bin, FD) summing by year and/or
>by area; For example, I need a new dataset:
>
>Bin,  FD[Year=2000,Area=1],
>FD[Year=2000,Area=2], FD[Year=2000],  FD[Year=2001,Area=1], etc.
>
>Any help really appreciated!!
>
>NG
>
>    [[alternative HTML version deleted]]
>
>
>______________________________________________
>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