Re: [R] how to subtotal by rows

2013-04-20 Thread shyam basnet
, 2013 12:52 AM Subject: Re: [R] how to subtotal by rows 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

[R] how to subtotal by rows

2013-04-19 Thread shyam basnet
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

Re: [R] how to subtotal by rows

2013-04-19 Thread Rui Barradas
Hello, Try the following. dat - read.table(text = fid year rice wheat maize 11995 5NA NA 11995 NA3 NA 11995 NA NA 2 11996 4NA NA 11996 NA

Re: [R] how to subtotal by rows

2013-04-19 Thread Janesh Devkota
Hello Shyam, This is one way to do it jd1 - read.table(text= fid year rice wheat maize 11995 5NA NA 11995 NA3 NA 11995 NA NA 2 11996 4NA NA 11996

Re: [R] how to subtotal by rows

2013-04-19 Thread Janesh Devkota
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.devk...@gmail.comwrote: Hello Shyam, This is one way to do it jd1 - read.table(text= fid year rice wheat maize 1

Re: [R] how to subtotal by rows

2013-04-19 Thread Rui Barradas
Hello, You don't need as.matrix(), the following will do. aggregate(dat[, 3:5], by = list(dat$year, dat$fid), FUN = sum, na.rm = TRUE) Rui Barradas Em 19-04-2013 20:24, Rui Barradas escreveu: Hello, Try the following. dat - read.table(text = fid year rice wheat maize 1

Re: [R] how to subtotal by rows

2013-04-19 Thread arun
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

Re: [R] how to subtotal by rows

2013-04-19 Thread Janesh Devkota
8 4 #4 2 19967 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

Re: [R] how to subtotal by rows

2013-04-19 Thread arun
: 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.devk...@gmail.comwrote: Hello Shyam, This is one way to do

Re: [R] how to subtotal by rows

2013-04-19 Thread arun
- From: arun smartpink...@yahoo.com To: Janesh Devkota janesh.devk...@gmail.com Cc: R help r-help@r-project.org; shyam basnet shyamabc2...@yahoo.com; Rui Barradas ruipbarra...@sapo.pt Sent: Friday, April 19, 2013 6:25 PM Subject: Re: [R] how to subtotal by rows Hi Janesh