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

jim holtman jholtman at gmail.com
Thu Sep 15 20:20:44 CEST 2011

It would seem you want something like this:

new.data <- sapply(seq(10, 100, 10), function(i) sapply(vector, yourFunc, i))

On Thu, Sep 15, 2011 at 1:50 PM, Mike Treglia <mlt35 at tamu.edu> 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))){
>        new.data<-sapply(vector, function, i)
>    }
> 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.

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

More information about the R-help mailing list