[R] How to create a loop to test the condition?

arun smartpink111 at yahoo.com
Fri Apr 19 15:16:36 CEST 2013


Hi,
I am not sure I understand your question correctly.
set.seed(25)
mydata<- data.frame(vol3=sample(1:20,20,replace=TRUE),vol4=sample(5:45,20,replace=TRUE),vol15=sample(25:50,20,replace=TRUE),vol20=sample(30:60,20,replace=TRUE),week=rep(1:4,each=5))

funct<- function(target,data=NULL){
target<- eval(substitute(target),data,parent.frame())
 weeks<- 1:4; #change accordingly
 aggre<- aggregate(target~week,data,mean);
 plot(weeks,aggre$target,type="o")
target
 }

funct(target=vol3,data=mydata)
# [1]  9 14  3 18  3 20 13  7  2  6  7  8 20 12 14  3 11 15 10 15

 mydata$vol3
 #[1]  9 14  3 18  3 20 13  7  2  6  7  8 20 12 14  3 11 15 10 15
A.K.


>hi 
  >I have data with some random variable like vol3, vol4, vol15 and 
vol20. Here i need to group the data using aggregate so I created one 
function to >group the data.  Finally I need to check the variable using 
loop? 
>
>funct=function(target,data) { 
 > weeks=c(1:57); 
  >aggre<-aggregate(target~ week, data , mean); 
  >plot(weeks,aggre$target,type="o"); 
>} 
>funct(target=vol3,data=mydata) 
>funct(target=vol4,data=mydata) 
>funct(target=vol15,data=mydata) 
>funct(target=vol20,data=mydata)



More information about the R-help mailing list