[R] summing vectors

arun smartpink111 at yahoo.com
Thu Apr 4 16:08:33 CEST 2013


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