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]) ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.