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.