Hi, i get only one value, but expect ncol values - what's wrong ? test <- function(){ for (i in 1:ncol(configData)) x <- subset(configData, Q71 == 1 & configData[,i] ==1 , select = configData[,i]) y <- (dim(x)[1]/dim(configData)[1])*100 return(y) } thanks for advance & regards, Christian -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 27 Feb 2002, christian schulz wrote:> Hi, > i get only one value, but expect ncol values - what's wrong ?You only compute one value for y and then return it. The assignment to y is outside the loop, but in any case it would overwrite the previous value> test <- function(){ > for (i in 1:ncol(configData)) > x <- subset(configData, Q71 == 1 & configData[,i] ==1 , select > configData[,i]) > y <- (dim(x)[1]/dim(configData)[1])*100 > return(y) }My guess is that you mean something like test <- function(){ y<-numeric(ncol(configData)) for (i in 1:ncol(configData)){ x <- subset(configData, Q71 == 1 & configData[,i] ==1 , select = configData[,i]) y[i] <- (dim(x)[1]/dim(configData)[1])*100 } return(y) } -thomas Thomas Lumley Asst. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thanks, that's it and one big step for me to understand loops and functions in R Christian>On Wed, 27 Feb 2002, christian schulz wrote: > >> Hi, >> i get only one value, but expect ncol values - what's wrong ? > >You only compute one value for y and then return it. The assignment to y >is outside the loop, but in any case it would overwrite the previous value > >> test <- function(){ >> for (i in 1:ncol(configData)) >> x <- subset(configData, Q71 == 1 & configData[,i] ==1 , select >> configData[,i]) >> y <- (dim(x)[1]/dim(configData)[1])*100 >> return(y) } > >My guess is that you mean something like > test <- function(){ > y<-numeric(ncol(configData)) > for (i in 1:ncol(configData)){ > x <- subset(configData, Q71 == 1 & configData[,i] ==1 , select = configData[,i]) > y[i] <- (dim(x)[1]/dim(configData)[1])*100 > } > return(y) } > > > > -thomas > >Thomas Lumley Asst. Professor, Biostatistics >tlumley at u.washington.edu University of Washington, Seattle > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._