[R] how to subtotal by rows

Rui Barradas ruipbarradas at sapo.pt
Fri Apr 19 21:31:05 CEST 2013


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        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
> ", header = TRUE)
>
> aggregate(as.matrix(dat[, 3:5]), by = list(dat$year, dat$fid), FUN =
> sum, na.rm = TRUE)
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 19-04-2013 16:59, shyam basnet escreveu:
>>
>>
>> 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.
>>
>
> ______________________________________________
> 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