[R] Summing Select Columns of a Data Frame?

Julia Zhou ms.juliazhou at gmail.com
Tue Jan 20 20:12:55 CET 2009


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!




More information about the R-help mailing list