[R] how to subtotal by rows
arun
smartpink111 at yahoo.com
Fri Apr 19 21:43:34 CEST 2013
#or
ddply(jd1,.(fid,year),numcolwise(sum,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: 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:30 PM
Subject: Re: [R] how to subtotal by rows
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