Thanks, Arun. I was also trying to come up with that solution.
On Fri, Apr 19, 2013 at 2:43 PM, arun <smartpink...@yahoo.com> wrote: > #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.devk...@gmail.com> > To: shyam basnet <shyamabc2...@yahoo.com> > Cc: "r-help@R-project.org" <r-help@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 <shyamabc2...@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@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@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@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.