[R] inconsistent output when using variable substitution

Duncan Murdoch murdoch at stats.uwo.ca
Sat May 31 00:04:45 CEST 2008


On 30/05/2008 5:50 PM, Robert Felty wrote:
> Mark,
> 
> Thanks for the reply. 
> 
>> hi:  subset doesn't know what i  is but I don't have enough knowledge
>> about scope to know what it's actually doing in that case. my
>> point is that i wouldn't put i inside a subset command and expect it to
>> know the value. scope is  very complex in R so
>> doing things in the simplest manner is best like the way you do it with
>> 49. hopfully someone else will reply with
>> a more detaild answer but i have a feeling that that is where your
>> problem lies. good luck.
>>
> 
> I still get the wrong result without i:
> Subj[1]
> [1] "49"
>> thisSubj = subset(Trials,Trials$Subj=="49")
>> thisSubj$Ansr[1]
> [1] "able"
>> thisSubj = subset(Trials,Trials$Subj==Subj[1])
>> thisSubj$Ansr[1]
> [1] "abacus"
> 
> The problem is that I want to use this in a loop, so that I can get a score 
> for every subject. Maybe there is an alternative way to do this?

It's a scoping problem.  To be helpful, subset looks up variables within 
the dataframe first.  Since it can find Subj there, that's the one it 
uses in the test.

The easiest solution is just to make sure that Subj is named something 
that isn't a column of Trials, e.g. localSubj.  Then you could use

subset(Trials, Subj == localSubj[1])

(Notice we can skip Trials$ on the first Subj.)

If you want to write bulletproof code it's harder, because there's no 
way to tell subset() *not* to look in Trials for variables.  In that 
case I'd avoid using subset (which is just a convenience function, and 
use row indexing instead:

thisSubj <- Trials[Trials$Subj == Subj[1], ]

will give you what you expect.

Duncan Murdoch



More information about the R-help mailing list