[R] Using for (i in ...) command with function

Jennifer Emond jemond at ucsd.edu
Thu Jun 17 00:13:22 CEST 2004


Hi,

I have defined a function, and I want to apply this function for 
different values of i, yet, when I use a for command, it only
executes the function for the first value of i.

My functions are long, but here is an example.  The problem is if I run 
the following code:

tab<-function(data,x){
a.length<-length(data[,x])
return(a.length)
}

sum<-function(data,x){
b.sum<-sum(data[,x])
return(b.sum)
}

final <- function(data,c){
     if(is.factor(data[,x])==TRUE){
     tab(data,x)
   } else{
     sum(data,c)}
   }


Then try to run:
for (i in 1:10){
	final(data,i)
}

I get only the output for function for when i=1, and not for each value.

Thanks in advance,
Jenn


Jennifer A. Emond, MS
Statistician
Department of Biostatistics, UCSD
9500 Gilman Drive
M/C 0949
La Jolla, CA  92093-0949
(858) 622-5877
jemond at ucsd.edu




More information about the R-help mailing list