[R] Summing Select Columns of a Data Frame?
Stephan Kolassa
Stephan.Kolassa at gmx.de
Tue Jan 20 20:59:00 CET 2009
Hi Julia,
or you can use colSums on an appropriate sub-data.frame:
colSums(dataset[,c(1,2)])
HTH,
Stephan
Jorge Ivan Velez schrieb:
> Dear Julia,
> Try this:
>
> DF<-read.table(textConnection("
> x1 x2 x3
> 1 2 3
> 4 5 6
> 7 8 9"),header=TRUE)
> closeAllConnections()
>
> DF$x4<-DF$x1+DF$x2
> DF
>
>
> HTH,
>
> Jorge
>
>
>
> On Tue, Jan 20, 2009 at 2:12 PM, Julia Zhou <ms.juliazhou at gmail.com> wrote:
>
>> Hi,
>>
>> I would like to operate on certain columns in a dataframe, but not
>> others. My data looks like this:
>>
>> x1 x2 x3
>> 1 2 3
>> 4 5 6
>> 7 8 9
>>
>> I want to create a new column named x4 that is the sum of x1 and x2,
>> but NOT x3. I looked at colSums and apply, but those functions seem to
>> use all the columns in a dataframe. How do I only use select columns?
>>
>> If it helps, in Stata this would be gen x4 = x1 + x2.
>>
>> Thanks!
>>
>> ______________________________________________
>> 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.
>>
>
> [[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.
>
More information about the R-help
mailing list