I wanted to ask how I can make a for loop or a function return an R
object with a unique name based on either some XX of the for loop or
some input for the function.
For example
if I have a function:
fn<-function(data,year){
which does does some stuff
}
How do I return an object from the function called X.year, such that if
I run fn(data,1989), the output is an object called X.1989?
In a separate but related process, I'm also trying to subset data by
year, where there are multiple observations by years, using the subset()
function. For example:
data.1946<-subset(data, year==1946)
data.1947<-subset(data, year==1947)
data.1948<-subset(data, year==1948)
data.1949<-subset(data, year==1949)
...
How should I set this up? I was thinking of writing a for loop, but I
have never written a for loop that creates objects based on the loop's
index, for example a loop for(i in 1946:2000) that returns 55 objects
with the object names based on the index.
Thanks for your help!
function returns R object with name based on input
3 messages · Jennifer Brea, David Winsemius
On Apr 24, 2009, at 11:56 AM, Jennifer Brea wrote:
I wanted to ask how I can make a for loop or a function return an R
object with a unique name based on either some XX of the for loop or
some input for the function.
For example
if I have a function:
fn<-function(data,year){
which does does some stuff
}
How do I return an object from the function called X.year, such that
if I run fn(data,1989), the output is an object called X.1989?
Read: ?assign ?paste #and FAQ 7.21 http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f
In a separate but related process, I'm also trying to subset data by year, where there are multiple observations by years, using the subset() function. For example: data.1946<-subset(data, year==1946) data.1947<-subset(data, year==1947) data.1948<-subset(data, year==1948) data.1949<-subset(data, year==1949) ...
list.of.subsets <- sapply(1946:200, function(x) subset(data, year==x) ) # with no example ... untested Using data as a dataframe names is poor R programming practice, since many functions use data a a parameter name and it is also a function name.
How should I set this up? I was thinking of writing a for loop, but I have never written a for loop that creates objects based on the loop's index, for example a loop for(i in 1946:2000) that returns 55 objects with the object names based on the index.
David Winsemius, MD Heritage Laboratories West Hartford, CT
2 days later
Thanks for your replies. I ended up using the following:
df = data.frame(year =c(1991,1991,1992,1992,1993,1993,1992,1991),x=rnorm(8),y=rnorm(8)) df
year x y 1 1991 0.5565083 -1.31364232 2 1991 0.1686598 -0.20344656 3 1992 -0.1010090 -0.65681852 4 1992 0.6130324 -0.10788605 5 1993 -0.9061458 -0.64872139 6 1993 -0.4460332 0.07253762 7 1992 -0.3865464 -1.87445996 8 1991 0.9252679 0.14891506
dfs = split(df,df$year) dfs[['1991']]
year x y 1 1991 0.5565083 -1.3136423 2 1991 0.1686598 -0.2034466 8 1991 0.9252679 0.1489151
dfs[['1992']]
year x y 3 1992 -0.1010090 -0.6568185 4 1992 0.6130324 -0.1078861 7 1992 -0.3865464 -1.8744600 Notice that split automatically uses a character version of the values of the split variable to name its output. Once you've created the list, you can use sapply or lapply to process each piece. Let's say we wanted the regression coefficients for the regression of y on x for each year:
regs = sapply(dfs,function(d)coef(lm(y~x,data=d))) regs
1991 1992 1993 (Intercept) -0.6964841 -0.9456066 0.7717261 x 0.4370229 1.5752294 1.5675705
David Winsemius wrote:
On Apr 24, 2009, at 11:56 AM, Jennifer Brea wrote:
I wanted to ask how I can make a for loop or a function return an R
object with a unique name based on either some XX of the for loop or
some input for the function.
For example
if I have a function:
fn<-function(data,year){
which does does some stuff
}
How do I return an object from the function called X.year, such that
if I run fn(data,1989), the output is an object called X.1989?
Read: ?assign ?paste #and FAQ 7.21 http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f
In a separate but related process, I'm also trying to subset data by year, where there are multiple observations by years, using the subset() function. For example: data.1946<-subset(data, year==1946) data.1947<-subset(data, year==1947) data.1948<-subset(data, year==1948) data.1949<-subset(data, year==1949) ...
list.of.subsets <- sapply(1946:200, function(x) subset(data, year==x) ) # with no example ... untested Using data as a dataframe names is poor R programming practice, since many functions use data a a parameter name and it is also a function name.
How should I set this up? I was thinking of writing a for loop, but I have never written a for loop that creates objects based on the loop's index, for example a loop for(i in 1946:2000) that returns 55 objects with the object names based on the index.
David Winsemius, MD Heritage Laboratories West Hartford, CT