0

I have a config.R file which has variable P.

P_val<-function(s){   
if(s==1){    
return(p<-0.01)  
 }  
 else if(s==2){
     return(p<-0.031)  
 }  
 else if(s==3){
     return(p<-0.001)  
 }   
else if(s==4){
     return(p<-0.021) 
  }   
else if(s==5){
     return(p<-0.1)  
 }   
else if(s==6){
     return(p<-0.77)  
 }  
 else if(s==7){
     return(p<-0.35)   
}  
 else if(s==8){
     return(p<-0.66)   
}
 }

In my main.R file I want to use this P value but the thing is this p variable here is in a loop and I want different value for each loop run. I am showing you a sample demonstration of what I want:

  d<-function(num){
for(s in seq(1,8,1)){
  x=2*s ##some variable
  source("config.R")
  P_val(s)
  reset(x,p)
}
reset<-function(x,p){
  l_val= (x/p) * num
  return(l_val)
}
}

I am using source("config.R") in my main.R file but I don't know how to use it as I am getting this error

Error in reset(x, p) : object 'p' not found

Shivanshu
  • 3
  • 4
  • 1
    Welcome to SO! Please read on how to [ask a good question](http://stackoverflow.com/help/how-to-ask) and how to make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Also include expected output and code that you have tried so far. – Sotos Jan 02 '17 at 07:48
  • rather than discussing this unconstructive question through one of the answers, please try to improve the question itself. Add sample data, share what you expect as output etc. Go through SO questions to learn how to construct a question. – joel.wilson Jan 02 '17 at 08:09
  • 1
    you're not assigning the value returned by the function. Try `p = P_val(s)` ? – Aramis7d Jan 02 '17 at 10:17
  • Yes thank you. @Aramis7d – Shivanshu Jan 02 '17 at 10:35

2 Answers2

0

More explanations on the files and relations between i and p might be helpful here.

Assuming there is a relation between the iteration number i and value of p you can try defining this relation as a function within the config.R file.

Then you just need to source("config.R") wihtin your main.R, as you're already doing, and make a call to the function from within the iterator whenever needed.

Aramis7d
  • 2,444
  • 19
  • 25
  • It seems you're just defining them statically. Have you used a function? pass `i` to the function as an argument, then use `conditional` cases like `if-else` within the function to assign a value to `p` and return this value from the function. – Aramis7d Jan 02 '17 at 07:31
  • please edit the question to include the function definitions and such. would be more helpful. – Aramis7d Jan 02 '17 at 08:17
  • I did the editing @Aramis7d – Shivanshu Jan 02 '17 at 09:45
0

You have to assign the value p, like so:

p <- P_val(s)
ErrantBard
  • 1,421
  • 1
  • 21
  • 40