[R] Are loops handled differently in newer versions of R?
Michael Rennie
mdrennie at gmail.com
Wed Mar 17 03:53:27 CET 2010
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
More information about the R-help
mailing list