[R] ifelse statement

karena dr.jzhou at gmail.com
Wed Jul 7 23:58:23 CEST 2010


Hi, I am a newbie of R, and playing with the "ifelse" statement.

I have the following codes:
## first,

for(i in 1:3) {
for(j in 2:4) {
cor.temp <- cor(iris.allnum[,i], iris.allnum[,j])
if(i==1 & j==2) corr.iris <- cor.temp
else corr.iris <- c(corr.iris, cor.temp)
}
}

this code is working fine.

I also tried to perform the same thing in another way with "ifelse":
for(i in 1:3) {
for(j in 2:4) {
cor.temp <- cor(iris.allnum[,i], iris.allnum[,j])
corr.iris <- ifelse(i==1 & j==2, cor.temp, c(corr.iris, cor.temp))
}
}

This is not working. Seems the value of "c(corr.iris, cor.temp)" has not
been assigned to corr.iris, even  when the (i==1 & j==2) is not satisfied.

what's the problem here?

thanks,

karena
-- 
View this message in context: http://r.789695.n4.nabble.com/ifelse-statement-tp2281576p2281576.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list