Robert Felty
2008-May-30 19:59 UTC
[R] inconsistent output when using variable substitution
I am extremely puzzled by this behavior in R. I have a data frame called Trials in which I have results from an experiment. I am trying to do a subjects analysis, but getting weird results. Each row has 1 trial in it, which includes a column for the subject number I get the list of subject numbers like so:> Subj=unique(sort(Trials$Subj))Then I loop over them. But I get strange results. As a test, I tried the following:> i=1 > Subj[i][1] 49> thisSubj = subset(Trials,Trials$Subj==Subj[i]) > thisSubj$Ansr[1][1] "abacus"> thisSubj = subset(Trials,Trials$Subj==49) > thisSubj$Ansr[1][1] "able" I am expecting to get a result of "able" both times, since I know that Subj[i] is 49, when i=1. Clearly the two different "thisSubj=..." lines are not returning the same values, as I would expect them to. Is this the normal and correct behavior for R, or am I missing something? And if it is normal and correct, what do I need to do to get the behavior I am expecting. Thanks in advance for any advice. Rob -- Robert Felty http://robfelty.com "In this house we obey the laws of thermodynamics!" -- Homer Simpson
Robert Felty
2008-May-30 21:50 UTC
[R] inconsistent output when using variable substitution
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? Rob -- Robert Felty http://robfelty.com Reality is known to have a liberal bias. -- Stephen Colbert
Dieter Menne
2008-May-31 10:05 UTC
[R] inconsistent output when using variable substitution
Robert Felty <robfelty <at> indiana.edu> writes:> > I am extremely puzzled by this behavior in R. I have a data frame called > Trials in which I have results from an experiment. I am trying to do a > subjects analysis, but getting weird results. Each row has 1 trial in it, > which includes a column for the subject number I get the list of subject > numbers like so: > > Subj=unique(sort(Trials$Subj)) > Then I loop over them. But I get strange results. As a test, I tried the > following: > > i=1 > > Subj[i] > [1] 49 > > thisSubj = subset(Trials,Trials$Subj==Subj[i]) > > thisSubj$Ansr[1] > [1] "abacus" > > thisSubj = subset(Trials,Trials$Subj==49) > > thisSubj$Ansr[1] > [1] "able" > > I am expecting to get a result of "able" both times, since I know that Subj[i] > is 49, when i=1. Clearly the two different "thisSubj=..." lines are not > returning the same values, as I would expect them to.Yes, this is "expected behavior", but nevertheless sonfusing and a source of some unexpected errors. The sorting leads to a re-arrangement of the levels, so that Subj is a different beast after the sorting. Please, do post complete, self-running examples next time that can be pasted into RGui. It needs 2 minutes to find the problem in the example below, and 10 minutes to resolve the "what did he think" and remove the > Dieter -------------------------------------- Trials = data.frame(Subj=c(7,5,5,3,3,7), Ansr = letters[1:6]) Trials$Subj = as.factor(Trials$Subj) Subj = unique(Trials$Subj) str(Subj) # Factor w/ 3 levels "3","5","7": 3 2 1 Subj[1] subset(Trials, Trials$Subj==Subj[1]) Subj = sort(unique(Trials$Subj)) str(Subj) # Factor w/ 3 levels "3","5","7": 1 2 3 Subj[1] subset(Trials, Trials$Subj==Subj[1])