[R] Constructing variables conditional on two indicators

Reynaerts, Jo
Fri, 11 Sep 2009 07:23:13 -0000

Dear R users

I have a data frame that looks like this:

m         j           X1         X2

1          1
1          2
1          ...
1          J
2          1
2          2
2          ...
2          J
.
.
.
M         1
M         2
M         ...
M         J

The data frame essentially looks like a panel: m and j are indicators where m is
1:M and j is 1:J (and M > J); X1 and X2 are variables.  I would like to
construct and add new variables to the data frame that are m- and j- specific,
e.g., compute X3 where X3 is the sum of X1 of all j in m, and this for each m.
The new variable X3 is then added to the data frame (and is the same for all
rows with the same m indicator).

How can I program this efficiently without running loops?

Thanks in advance

Jo Reynaerts

Ph.D. student
LICOS Centre for Institutions and Economic Performance
Katholieke Universiteit Leuven
Debériotstraat 34/3511
B-3000 Leuven
Belgium
Jo.Reynaerts@econ.kuleuven.be<mailto:Jo.Reynaerts@econ.kuleuven.be>




	[[alternative HTML version deleted]]