Mike Treglia
2011-Sep-15 17:50 UTC
[R] Problem looping a function to create/add to new dataframe
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
jim holtman
2011-Sep-15 18:20 UTC
[R] Problem looping a function to create/add to new dataframe
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?
David Winsemius
2011-Sep-15 18:40 UTC
[R] Problem looping a function to create/add to new dataframe
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
Apparently Analagous Threads
- All possible combinations of functions within a function
- [off topic] [research] Interviews for contributors over 50 for Oregon State University research
- How do I change plot colors in bwplot
- Error "singular gradient matrix at initial parameter estimates" in nls
- Visiting Researcher, species distribution modelling, Cali, Colombia