Hi again Julia,
Here are two more options:

DF<-read.table(textConnection("
x1 x2 x3
1   2   3
4   5   6
7   8   9"),header=TRUE)
closeAllConnections()

# Option 1  -- sent before
DF$x1+DF$x2

# Option 2
apply(DF,1,function(x) x[1]+x[2])

# Option 3
rowSums(DF[,c(1,2)])


HTH,

Jorge


On Tue, Jan 20, 2009 at 2:49 PM, Jorge Ivan Velez
<jorgeivanve...@gmail.com>wrote:

>
> 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.juliaz...@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@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@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.

Reply via email to