[R] Adding a calculated variable to a data frame

Andrew Perrin clists at perrin.socsci.unc.edu
Tue Jun 24 17:19:19 CEST 2003


Is there a shortcut way to add a calculated variable to a data frame? For
example, I have a data frame with variables first.conv, first.sub, and
first.agg. Each cell is -1, 0, or 1. I'd like to generate variables:

rwa.sum  : sum(first.agg,first.sub,first.conv)
rwa.psum : total number (0-3) of vars == 1
rwa.asum : total number (0-3) of vars == -1
rwa.val  : sum(abs(first.agg,first.sub,first.conv))

The only way I can think of to do this is to create a separate data.frame
to hold the calculated values, then use cbind() to paste the two together.
Am I missing an easier option?

Thanks.

----------------------------------------------------------------------
Andrew J Perrin - http://www.unc.edu/~aperrin
Assistant Professor of Sociology, U of North Carolina, Chapel Hill
clists at perrin.socsci.unc.edu * andrew_perrin (at) unc.edu




More information about the R-help mailing list