Re: [R] How to evaluate a Variable Name?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri 20 Oct 2006 - 15:19:55 GMT

Create a function f which, given Species s, calculates the residual sum of squares and then sapply it to all Species. In your case Species is the month and the levels of the Species correspond to the months 1:12

# calculate rss for Species s
f <- function(s) {

   iris.lm <- lm(Sepal.Width ~ Sepal.Length, iris, subset = Species == s)    sum(resid(iris.lm)^2)
}

sapply(levels(iris$Species), f)

Please see the last line of every message to r-help and in particular provide reproducible code in your postings. That means either providing a snippet of your data or using an analogous builtin dataset.

On 10/20/06, Dan Chan <dchan@gfc.state.ga.us> wrote:
> Hi,
>
>
>
> I have a dataframe Wash2005, which has daily weather data. I am doing a
> regression by month as follows:
>
> # FM10 Regression by Month
>
> # Plot 12 Month of OFM10, FFM10

>
> for(i in 1:12) {
>
> Temp <- subset (Wash2005, MM == i)
>
> assign( paste('Wash2005FM10', strtrim(month.name[i],3),
> sep=""),lm(Temp$FM10.1 ~ Temp$FM10))
>
> }
>
>
>
> Wash2005FM10Jan, Wash2005FM10Feb, etc holds the regression results for
> each month.
>
>
>
> Now, I want to sum up the residuals of all the 12 regression. How can I
> do that in a loop?
>
> For (i in 1:12){
>
> Temp <- paste('sum(Wash2005FM10', strtrim(month.name[i],3),
> '$residual**2)',sep="")
>
> }
>
>
>
> Temp will give a string "sum(Wash2005FM10Jan$residual**2)". How can I
> get R to evaluate this string?
>
>
>
> Thank you.
>
>
>
>
>
> Daniel Chan
>
> Meteorologist
>
> Georgia Forestry Commission
>
> P O Box 819
>
> Macon, GA
>
> 31202
>
> Tel: 478-751-3508
>
> Fax: 478-751-3465
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>



R-help@stat.math.ethz.ch 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. Received on Sat Oct 21 08:22:12 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 20 Oct 2006 - 22:30:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.