[R] Are loops handled differently in newer versions of R?

Joshua Wiley jwiley.psych at gmail.com
Wed Mar 17 04:57:28 CET 2010


Michael,

I have to agree with Don that using a factor as a loop index seems
like a risky choice.  At any rate, part of the problem is that you are
referencing a nonexistant part of your dataframe.  i is an index of
characters, but your rownames are 1:5, not LETTERS[1:5].  If you give
your dataframe rownames, you can then use your loop, see below.

########################
locn<-c("A", "B", "C", "D", "E")
n<-c(28, 14, 21, 52, 35)
corr.r<-c(0.40, 0.63, 0.38, 0.44, 0.35)

lab8.dat<-data.frame(locn, n, corr.r)
lab8.dat

calc.prob.t<-function(n, r)
   {
   df<-n-2
   t<-(r-0)/(sqrt((1-r^2)/df)) # I'm assuming you mean r^2 here not r2
       probt<-2*(pt(t, df, lower.tail=FALSE))
   probt
   }

p_unadj<-NULL # since you assign it to null anyways, there's not real
point in the other assignment
p_unadj

for ( i in lab8.dat[,1] )
       p_unadj[i] <- calc.prob.t(lab8.dat[i,2], lab8.dat[i,3])

p_unadj # all NAs as you noticed

rownames(lab8.dat) <- lab8.dat$locn

for ( i in lab8.dat[,1] )
       p_unadj[i] <- calc.prob.t(lab8.dat[i,2], lab8.dat[i,3])

p_unadj # now lab8.dat["A",2] etc. means something, and it works

##############################




On Tue, Mar 16, 2010 at 7:53 PM, Michael Rennie <mdrennie at gmail.com> wrote:
>
> Hi gang,
>
> I'm perplexed- I have some code that uses for() loops that works fine in R
> version 2.8 on my mac, worked fine in version 2.8 on my old windows machine,
> but doesn't work in version 2.10 on windows.
>
> The loop implements a function over a data frame (code is included below).
>
> In Mac (running version 2.8), the results of the loop are what I expect:
>
>> p_unadj
> [1] 0.034939481 0.015743706 0.089287030 0.001098538 0.039290594
>
> But in Windows (running version 2.10.1), I get a bunch of NA's...
>
>> p_unadj
>  A   B      C    D    E
> NA NA NA NA NA
>
> If I had to guess, I'd say that R v. 2.10 is handling the i in lab8.dat[,1]
> differently, given that it's keeping the row names in the output for
> p_unadj... but why would that stop it from applying the function?
>
> Any thoughts or suggestions are welcome.
>
> Cheers,
>
> Mike
>
> Here's the code...
>
>
> #build the dataset
>
> locn<-c("A", "B", "C", "D", "E")
> n<-c(28, 14, 21, 52, 35)
> corr.r<-c(0.40, 0.63, 0.38, 0.44, 0.35)
>
> lab8.dat<-data.frame(locn, n, corr.r)
> lab8.dat
>
> attach(lab8.dat)
>
> #write the function
>
> calc.prob.t<-function(n, r)
> #given a sample size (n) and correlation coefficient (r), returns the
> probability for that test
>    {
>    df<-n-2
>    t<-(r-0)/(sqrt((1-r2)/df))
>        probt<-2*(pt(t, df, lower.tail=FALSE))
>    probt
>    }
>
> #try out the function...
> calc.prob.t(lab8.dat$n[1], lab8.dat$corr.r[1])
> #it works.
>
> #write a loop to implement that function for every correlation in your
> dataset...
>
>
> p_unadj<-numeric(length(lab8.dat[,1]))
> p_unadj<-NULL
> p_unadj
>
> #all this just built an empty vector to store the results of our loop...
>
> for ( i in lab8.dat[,1] )
>        p_unadj[i]<-calc.prob.t(lab8.dat[i,2], lab8.dat[i,3])
>
> p_unadj
>
> #if executed on my Mac, running R v.2.8, this works (and did using 2.8 on my
> old windows machine). Running v. 2.10 in Windows, I get NAs.
>
> --
> Michael D. Rennie, Ph.D.
> Postdoctoral Fellow, Environmental and Life Sciences Program
> Trent University
> 2140 East Bank Drive, DNA Building (2nd Floor)
> Peterborough, Ontario K9J 7B8
> Vox:705.755.2287 Fax:705.755.1559
> www.people.trentu.ca/michaelrennie
>
> ______________________________________________
> R-help at 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.
>



-- 
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/



More information about the R-help mailing list