[R] variable substitution in for loops

Jon Erik Ween jween at klaru-baycrest.on.ca
Thu Feb 25 03:18:38 CET 2010


David

Thanks for your suggestions. "Reproducible examples", my datasets are too
big so I'm not sure what you would want to see.  Anyway, does this help:

dataset:
Perf_Index Age risk_score gender hight IQ ...
12 78 10 m 6 110 ...
10 77 9 f 6 97 ...
5 64 f 13 5 120 ...
...

varslist:
Perf_Index Age IQ ...


As you suggested

> str(varslist)
'data.frame':	1 obs. of  54 variables:
 $ V1 : Factor w/ 1 level "Perf_Index": 1
 $ V2 : Factor w/ 1 level "Age": 1
 $ V3 : Factor w/ 1 level "risk_score": 1
...

for (i in varslist){
	v<-mean(Dataset[,i])
	print(v)
	}

gives: 

> source("/Users/jween/Desktop/test.R")
[1] 20016.06
[1] 20016.06
[1] 20016.06
[1] 20016.06
[1] 20016.06
[1] 20016.06
... 54 instances, the number of variables in varslist

Not correct, obviously. I'm guessing I've not read varslist in correctly (I
used read.table).

Cheers

Jon
-- 
View this message in context: http://n4.nabble.com/variable-substitution-in-for-loops-tp1568384p1568429.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list