Hi, I have some values in a list format generated by the following: Path_Number <- 0010 ID.Path <- formatC(0001:Path_Number, width=4, flag=0) # Make vector of ID's. No_of_Effectors <- sample(1:550, length(ID.Path), replace=TRUE) # Define Number of Effectors each individual gets. Effectors <- split(sample(1:10000, sum(No_of_Effectors), replace=TRUE), rep(ID.Path, No_of_Effectors)) # Generate effectors and dish them out. Effectors
And I've written a chunk which is designed to go through each element of the list, and then through each value in the element, and if a conditions is met (in this case if runif(1) is < 0.3, this is simple but may be changed later to be more complex probability criteria based on mutation data from experiment), change the value by 1 in either direction, higher or lower with equal probability. Here it is (I've changed the 0.3 value I mentioned to 0.9, so many values change so it can be easily seen): l<-0 # Set counter 1 to 0. for(i in Effectors){ # Begin loop on list of effectors. l2<-0 # Set counter 2 to 0. l <-l+1 # Increace counter number 1. for(i in Effectors[[l]]){ # Begin loop through all effector values. l2 <-l2+1 # Increace counter number 2. if(runif(1) < 0.9) ifelse(runif(1) <0.5, Effectors[[l]][l2] <- Effectors[[l]][l2]+1, Effectors[[l]][l2] <- Effectors[[l]][l2]-1) # Line which increaces or decreaces the values in the list element (50/50 chance of increace or decreace), if the first IF statement is satisfied. } } Now I don't know if this is the best and most R-ish way of doing this, but it works and I understand it. However I'd like to define a function with this, my attempts so far have been: Eff.Mutate<-function(){ l<-0 # Set counter 1 to 0. for(i in Effectors){ # Begin loop on list of effectors. l2<-0 # Set counter 2 to 0. l <-l+1 # Increace counter number 1. for(i in Effectors[[l]]){ # Begin loop through all effector values. l2 <-l2+1 # Increace counter number 2. if(runif(1) < 0.9) ifelse(runif(1) <0.5, Effectors[[l]][l2] <- Effectors[[l]][l2]+1, Effectors[[l]][l2] <- Effectors[[l]][l2]-1) # Line which increaces or decreaces the values in effvec, if the first IF statement is satisfied. } } } and: Eff.Mutate2<-function(x){ l<-0 for(i in x){ l2<-0 l<-l+1 for(i in x[[l]]){ l2<-l2+1 if(runif(1) < 0.9) ifelse(runif(1) <0.5, x[[l]][l2] <- x[[l]][l2]+1, x[[l]][l2] <- x[[l]][l2]-1) } } } However if I do either Eff.Mutate() or Eff.Mutate2(Effectors), then neither seems to work; I've seen no differences in the values in the list elements, before and after. I can't figure out why it works as a code chunk but if I try to make it a function nothing seems to happen. I'm probably going about making it a function wrong. Thanks, Ben W. UEA (ENV) & The Sainsbury Laboratory. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.