[R] Loop over several variables

bchr bochristoph at web.de
Thu Nov 1 12:32:07 CET 2012


Hey everybody,

I am looking for a way to loop commands over several variables in a
dataframe. Basically I want to do something like this:

ti1<-aggregate(dataframename$y1,
               by=data.frame(dataframename$aggregationvar),
               sum,na.rm=TRUE)

This works fine as it is but i want to do it for several variables thereby
generating several tix. I tried with a for-loop but the problem was that I
could neither find a way combine my indexnumber i (1 ... x) with the y or ti
(as for example in Stata I could do by writing y`i')  nor did it work using
a vector of string variables  ("y1", ... "yx") and looping over that (while
using yx also as a name for the target dataframe instead of tix - i would'nt
mind that).

Preferably I would be looking for a solution that can do without any of the
apply functions (yes, I know they are more R-like, but frankly, I don't get
the logic behind them, so for the time being I would prefer another way)

Tanks very much for your help

Bernhard



--
View this message in context: http://r.789695.n4.nabble.com/Loop-over-several-variables-tp4648112.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list