[R] variable substitution in for loops

Peter Ehlers ehlers at ucalgary.ca
Thu Feb 25 04:34:52 CET 2010


Jon Erik,

I don't know where you get 'varslist', but if it's from a text file,
then why not scan() it into a *vector* instead of using (I assume)
read.table() to create a data.frame. You'll save yourself much grief.

  -Peter Ehlers

On 2010-02-24 20:19, David Winsemius wrote:
>
> On Feb 24, 2010, at 9:18 PM, Jon Erik Ween wrote:
>
>>
>> David
>>
>> Thanks for your suggestions. "Reproducible examples", my datasets are too
>> big so I'm not sure what you would want to see.
>
> I _wanted_ to see a datasset that had maybe 10 variables and 20 rows and
> a stripped down varslist that had maybe half that number of variable names.
>
>
>> 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 ...
>> ...
>
> You _could_ have offered str(dataset)
>
>>
>> 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
>> ...
>
> And it does explain why you were not getting the results you were
> expecting. "varslist" is not a simple list. You have a dataframe (a
> special sot of list) of 54 single element factors, rather than either a
> vector with 54 elements or a list with 54 character elements.
>
>>
>> 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.
>
>
> Obviously, ... but not my fault. You are the one who constructed
> varslist that way. The factors are being interpreted as numbers and
> since each of them have only a single element, they are all the number
> 1. You might try to wrap as.character() around the call to varslist in
> the "in" phrase. That way the _levels_ of the factors would be returned,
> rather than their representation index.
>

-- 
Peter Ehlers
University of Calgary



More information about the R-help mailing list