Mikayla Baer
Mikayla Baer

Reputation: 49

How do I isolate a level as its own variable in R?

Within the dataset "PlantGrowth" there is only two variables, group and weight.

data("PlantGrowth")
names(PlantGrowth)
[1] "weight" "group" 

The variable "group" 3 levels

levels(PlantGrowth$group) 
[1] "ctrl" "trt1" "trt2"

I want to be able to call the control and treatments of the variable "group" but I can't figure out how to separate them into their own variable. I've renamed the levels to:

levels(PlantGrowth$group) <- c("control","treatment1","treatment2")

But now I want to be able to analyze control against treatment 1 and 2. I've tried a bunch of different things like this:

control <- (PlantGrowth$group$control)


Error in PlantGrowth$group$control : 
$ operator is invalid for atomic vectors

Obviously that is incorrect though Thank you!

Upvotes: 0

Views: 2697

Answers (3)

Aleh
Aleh

Reputation: 826

In case you want to run separate models for each group and compare results you can use following:

by(data = PlantGrowth, INDICES = PlantGrowth$group, FUN = summary)

or you can also build a matrix of regressors and use those:

model.matrix(~PlantGrowth$group)

Upvotes: 2

David Foster
David Foster

Reputation: 461

Surely most analysis functions would be able to use the group factor to split the data treatments, without you splitting up the dataset?

Not certain what comparison you'd like to make, but for example an ANOVA of your data:

ANOVA <- aov(weight ~ group, data=PlantGrowth)

Where the function uses your group variable to check for differences in weight.

Similarly when graphing, PlantGrowth$group could be applied to colour or shape appropriate points on the graph.

Upvotes: 0

Leonardo Siqueira
Leonardo Siqueira

Reputation: 371

Is this what you want? (using filter from dplyr)

control <- PlantGrowth %>% filter(group == "control")

if you want you can also split your whole data frame by "group", like so:

split(PlantGrowth, PlantGrowth$group)

Upvotes: 3

Related Questions