[R] Creating composite factor and changing format from character to factor
David Winsemius
dwinsemius at comcast.net
Sat Jul 16 16:50:25 CEST 2011
On Jul 16, 2011, at 10:21 AM, B Jessop wrote:
> Dear Help-list, I have a dataframe containing 6 variables, 4 of
> which are factors, 2 numeric. I want to create another factor
> variable (SitePos) by combining 2 existing factors (Site and
> Position). I have tried a number of approaches based on trolling
> the R FAQs, various R webpages, etc., none of which work. One
> approach e.g. Data1$SitePos <- paste(Data1$Site, Data1$Position)
> creates the appropriate "SitePos" values e.g. "CR core" but as
> character values not as a factor.
In one line you could have written:
Data1$SitePos <- factor( paste(Data1$Site, Data1$Position) )
Or you could have created an interaction:
Data1$SitePos <- interaction(Data1$Site, Data1$Position)
> A linear model run on the updated dataframe works but notes that it
> coerces "SitePos" from character to factor e.g. Model.G =
> lmer(log10(SrCa) ~ SitePos + (1 | Eel), data = Data1) . The next
> step of a multiple comparison test on the output of the linear
> model: Model.G.mct = glht(Model.G, linfct = mcp(SitePos = "Tukey"))
> fails because the mct does not recognize "SitePos" as a factor and
> gives error message: "Error in mcp2matrix (model, linfct = linfct):
> Variable(s) 'SitePos' of class 'character' is/are n!
> ot contained as a factor in 'model'. My final step is outputting
> the mct results: summary (Model.G.mct, test = adjusted(type =
> "single-step")). Any suggestions as to how to create a composite
> factor variable in the dataframe that would permit execution of the
> analysis code would be much appreciated. I am using R version
> 2.13.0 with packages lme4 and multcomp loaded. Regards,B Jessop
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
More information about the R-help
mailing list