[R] sum two columns with same value
Peter Ehlers
ehlers at ucalgary.ca
Sat Oct 17 15:39:59 CEST 2009
smu wrote:
> On Sat, Oct 17, 2009 at 09:36:50AM +0200, Alfredo Alessandrini wrote:
>> Hi,
>>
>> I've two dataframe:
>>
>>> snag_totale
>> AREA snag_ha
>> 1 2 1.628128
>> 2 3 10.274249
>> 3 4 2.778503
>> 4 5 73.764307
>> 5 7 12.015985
>>> log_totale
>> AREA log_ha
>> 1 1 22.29846
>> 2 2 17.16889
>> 3 3 48.80377
>> 4 4 144.18996
>> 5 5 70.30962
>> 6 6 61.81850
>> 7 7 13.24876
>> How can I obtain a new data.frame, by the sum of value "snag_ha" +
>> "log_ha" in the same "AREA"?
>
>
> one way would be:
>
> tmp <- merge(snag_totale, log_totale, by="AREA")
> tmp$sum <- tmp$snag_ha + tmp$log_ha
That will drop the missing rows in snag_totale.
Here's a variation that should work:
tmp <- merge(snag_totale, log_totale, all = TRUE)
tmp$Sum <- rowSums(tmp[, -1], na.rm = TRUE)
tmp
-Peter Ehlers
>
> regards,
> stefan
>
> ______________________________________________
> 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