[R] Add sums across chosen columns. ... was (No Subject)
David Winsemius
dwinsemius at comcast.net
Fri Aug 23 19:41:00 CEST 2013
On Aug 23, 2013, at 9:41 AM, Anindya Sankar Dey wrote:
> You can easily subset the data then use rowSum.
>
> say your dataset name is data1.
>
> then write data2<-data[,c(7,12,45,57)]
>
> then write result<-rowsum(data2)
In R there are two different functions `rowsum` and `rowSums`. The use you are describing sounds like a case rowSums, rather than `rowsum`. For reasons that are unclear to me `rowsum` seems to be more like `grouped.colSums` than anything I would have imagined it to be just looking at its name and then looking at its help page.
I think you wanted:
dfrm$m <- rowSums( dfrm[, c(7,12,57,45) ] ) # assuming dfrm is a dataframe.
--
David.
>
>
> On Fri, Aug 23, 2013 at 3:47 PM, rajib prasad <rwho2007 at gmail.com> wrote:
>
>> I am new to R. I have a data like:
>>
>> x y z w p ..........
>> m
>> 1 10 15 20 25 30
>> 2 11 16 21 26 31
>> 3 12 17 18 19 20
>> 4 51 52 53 55 67
>> .......
>>
>> thus I have 145 rows and 160 column in my data which is named as
>> data.csv. Now i want to create a new column 'm' and for every row m
>> will take value =column 7+ column 12+ column 57+ column 45 i.e. for
>> every row it will take value of sum of corresponding row's 7 & 12 & 57
>> & 45 column's value .
>> So, how to write the code for this operation?
>>
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list