[R] Dealing with data

TGS cran.questions at gmail.com
Fri Aug 13 19:51:13 CEST 2010


# Greg, if R automatically does that then I don't know why it's treating each indicator
# as a different regressor. In other words, I am interested in treating 'spray' as one
# independent variable.
# 
# Erik, which book do you suggest I read? Thanks.

data(InsectSprays)
lm(InsectSprays$count ~ 0 + InsectSprays$spray)

On Aug 13, 2010, at 10:34 AM, Greg Snow wrote:

R/S does all of that automatically for you, you do not need to manually create the indicator variables.

If you do something like:

> fit <- lm( Sepal.Width ~ Species, data=iris, x=TRUE)

Then look at the matrix actually used:

> fit$x

Or the output:

> summary(fit)

You will see that Species was automatically converted into indicator variables and those were used in the regression.

If you really need the indicator variables yourself, look at the model.matrix function, e.g.:

> model.matrix( ~Species, data=iris )

Or

> model.matrix( ~Species - 1, data=iris )

If you really want 1 for A, 2 for B, etc. then look at as.numeric on a factor variable (e.g. as.numeric(iris$Species) ).

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of TGS
> Sent: Friday, August 13, 2010 11:22 AM
> To: David Winsemius
> Cc: r-help at r-project.org
> Subject: Re: [R] Dealing with data
> 
> To clarify, I'd like to create a column of indicators for the
> respective letters so that I could maybe do regression on indicators,
> etc.
> 
> For instance, "A" gets "1", "B" gets "2", and so on.
> 
> On Aug 13, 2010, at 10:19 AM, David Winsemius wrote:
> 
> 
> On Aug 13, 2010, at 1:03 PM, TGS wrote:
> 
>> # how would I code in R to look at the letter of the alphabet
>> # in the second column and create a indicator column for the
>> # corresponding letter?
>> 
>> data(InsectSprays)
>> InsectSprays$spray
> 
> It's already what most people mean when they say "indicator column",
> i.e., a factor variable (and not a character vector) .... so,  what do
> _you_ mean?
>> 
> 
> 
> --
> 
> David Winsemius, MD
> West Hartford, CT
> 
> ______________________________________________
> 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.



More information about the R-help mailing list