[R] Problem looping a function to create/add to new dataframe

Michael L. Treglia mlt35 at tamu.edu
Thu Sep 15 21:58:36 CEST 2011

Thank you all- with your suggestions I was able to get this simulation 
to run- sorry, next time I'll post some reproduceable code- it had been 
a while since I posted, so I forgot about that.

On 9/15/2011 1:40 PM, David Winsemius wrote:
> On Sep 15, 2011, at 1:50 PM, Mike Treglia wrote:
>> Dear List Members,
>> I have created a function to run a simulation based on a given set of 
>> values within a vector, such that I run the function like this:
>> new.data<-sapply(vector, function)
>> In which I run 'function' on every value within a vector that I 
>> created. The result is a matrix of 1000 rows, and as many columns as 
>> the length of the vector.
>> I modified the function to utilize two given values, such that it is: 
>> function(x,y), and I want to run this function for not only a range 
>> of values of 'x', but a range of values of 'y' as well, such that for 
>> each value of 'y', I'd create another 1000 rows in the matrix. I was 
>> trying to loop this, but in doing so, it looks like I just keep 
>> creating datasets and replacing them with datasets for subsequent 'y' 
>> values, rather than adding new rows. The structure of my current loop 
>> is below. Any suggestions on what to change to accomplish what I 
>> want? Would it be good to create a dataframe first and then somehow 
>> add these new rows to the dataframe? Also, is it appropriate to have 
>> the 'i' in the sapply statement?
>> for (i in c(seq(10,100,10))){
> The c() call is extraneous.
>>        new.data<-sapply(vector, function, i)
> Perhaps:
> # scrap the entire for loop(sapply()) mess and use this
> newdata <- outer(Y=seq(10,100,10), X=vector, FUN=function)
> Or:
> mapply(function,  expand.grid(x=vector, y=seq(10,100,10) ) )
> # which is sort of like your last notion,
> All of this is untested in the absence of a reproducible example but 
> you definitely should work through the examples on:
> ?outer
> ?mapply
>>    }
>> Please let me know if more detail on my code would be helpful- I was 
>> just trying to keep it simple and focus on what I saw as the problem 
>> at hand for now.
>> Thank you for your help.
>> Sincerely,
>> Mike Treglia
>> -- 
>> Michael Treglia
>> Applied Biodiversity Sciences NSF-IGERT Program
>> Wildlife and Fisheries Sciences
>> Texas A&M University
>> Lab: (979)862-7245
>> mlt35 at tamu.edu
>> http://people.tamu.edu/~mlt35
>> ______________________________________________
>> R-help at 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.
> David Winsemius, MD
> West Hartford, CT

More information about the R-help mailing list