[R] how to subtotal by rows
arun
smartpink111 at yahoo.com
Sat Apr 20 00:52:19 CEST 2013
HI,
If you wanted to use colSums, may be this works:
ddply(jd1,.(fid,year),function(x) colSums(x[,-c(1,2)],na.rm=TRUE))
# fid year rice wheat maize
#1 1 1995 5 3 2
#2 1 1996 4 2 6
#3 2 1995 3 8 4
#4 2 1996 7 6 7
A.K.
----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Janesh Devkota <janesh.devkota at gmail.com>
Cc: R help <r-help at r-project.org>; shyam basnet <shyamabc2002 at yahoo.com>; Rui Barradas <ruipbarradas at sapo.pt>
Sent: Friday, April 19, 2013 6:25 PM
Subject: Re: [R] how to subtotal by rows
Hi Janesh,
There is a difference in output between these :
ddply(jd1,.(fid,year),colSums,na.rm=T) #especially the first two columns
# fid year rice wheat maize
#1 3 5985 5 3 2
#2 3 5988 4 2 6
#3 6 5985 3 8 4
#4 6 5988 7 6 7
ddply(jd1,.(fid,year),colwise(sum,na.rm=T))
# fid year rice wheat maize
#1 1 1995 5 3 2
#2 1 1996 4 2 6
#3 2 1995 3 8 4
#4 2 1996 7 6 7
aggregate(jd1[,3:5],by=list(jd1$year,jd1$fid),FUN=sum,na.rm=TRUE)
# Group.1 Group.2 rice wheat maize
#1 1995 1 5 3 2
#2 1996 1 4 2 6
#3 1995 2 3 8 4
#4 1996 2 7 6 7
A.K.
________________________________
From: Janesh Devkota <janesh.devkota at gmail.com>
To: shyam basnet <shyamabc2002 at yahoo.com>
Cc: "r-help at R-project.org" <r-help at r-project.org>
Sent: Friday, April 19, 2013 3:32 PM
Subject: Re: [R] how to subtotal by rows
You can also use this short command.
library(plyr)
ddply(jd1,.(fid,year),colSums,na.rm=T)
Janesh
On Fri, Apr 19, 2013 at 2:30 PM, Janesh Devkota <janesh.devkota at gmail.com>wrote:
> Hello Shyam,
>
> This is one way to do it
>
> jd1 <- read.table(text="
> fid year rice wheat maize
> 1 1995 5 NA NA
> 1 1995 NA 3 NA
> 1 1995 NA NA 2
> 1 1996 4 NA NA
> 1 1996 NA 2 NA
> 1 1996 NA NA 6
> 2 1995 3 NA NA
> 2 1995 NA 8 NA
> 2 1995 NA NA 4
> 2 1996 7 NA NA
> 2 1996 NA 6 NA
> 2 1996 NA NA 7
> ", sep="", header=T)
> jd1
>
> library(plyr)
>
> ddply(jd1,.(fid,year),summarise,
> rice=sum(rice,na.rm=T),wheat=sum(wheat,na.rm=T),maize=sum(maize,na.rm=T))
>
> Good luck
>
> Janesh
>
>
> On Fri, Apr 19, 2013 at 10:59 AM, shyam basnet <shyamabc2002 at yahoo.com>wrote:
>
>>
>>
>> Dear R-users,
>>
>> I have a dataset as like below, and I want to subtotal the values of
>> rice,wheat and maize by year for each fid.
>>
>> fid year rice wheat maize
>> ------------------------------------------------
>> 1 1995 5 NA NA
>> 1 1995 NA 3 NA
>> 1 1995 NA NA 2
>> 1 1996 4 NA NA
>> 1 1996 NA 2 NA
>> 1 1996 NA NA 6
>> 2 1995 3 NA NA
>> 2 1995 NA 8 NA
>> 2 1995 NA NA 4
>> 2 1996 7 NA NA
>> 2 1996 NA 6 NA
>> 2 1996 NA NA
>> 7-----------------------------------------------
>>
>> And, my output should look like below:
>>
>> fid year rice wheat maize
>> 1 1995 5 3 2
>> 1 1996 4 2 6
>>
>> 2 1995 3 8 4
>> 2 1996 7 6 7I am looking for some ideas or
>> r-codes on resolving my problem.
>> I appreciate your kind help,
>>
>>
>> Thanks a lot,
>>
>> Sincerely yours,
>> Shyam
>> Nepal
>> [[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.
>>
>>
>
[[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