How to make a subset that isn't a list?

Hi, so I'm pretty new to R in general. This is the data I'm using-

'data.frame': 40 obs. of 4 variables:

grp: Factor w/ 4 levels "1","2","3","4": 1 2 3 4 1 2 3 4 1 2 ... befored:
afterd : surviv : num 13 69.8 87.5 19.5 18.4 ...

What I want to do is to test if there are significant differences between group survival (surviv) using binomial glm

myfm <- glm(grp~surviva,family=binomial,data=d) = like this

but I want to see which groups specifically are different to each other, and create subsets using a glm to do pairwise testing between groups.

g1 <- subset(d, grp== "1" )
g2 <- subset(d, grp== "2" )
g12 <- subset(d, grp%in% c("1", "2") ) =this is what I did

However, when I try to put it in a glm I get this message which tells me I can't use the subset since it's a list?

fm <- glm(g12~surviva,family=binomial,data=d)

Error in model.frame.default(formula = g12 ~ surviva, data = d, drop.unused.levels = TRUE) :
invalid type (list) for variable 'g12'

I just don't really understand what to do to make it work.

Welcome to the community!

If I understand you correctly, you want to fit a model based on the observations only in 1st and 2nd group. Is that right?

Then, the following is supposed to work with no problem:

g12 <- subset(x = d, subset = (grp %in% c("1", "2")))
glm(formula = (grp ~ surviva), family = binomial, data = g12)

Can you please give it a try?

If it doesn't solve your problem, can you please provide a REPRoducible EXample of your problem? It provides more specifics of your problem, and it helps others to understand what problem you are facing.

In case you don't know how to make a reprex, here's a great link:

1 Like

Hi! Wow that helped a lot, thank you! The only thing is that while the g12 successfully created a subset- the second line spits out an error message that says:

Error in model.frame.default(formula = ( grp ~ surviva), data = s12, :
variable lengths differ (found for 'surviva')

But my number of observations for each variable is the same? (I think that's what they're saying right?)

NO WAIT DON'T WORRY I SOLVED IT (since my surviva object was a ratio using my original dataset I had to alter it to use the subset data instead!) THANKS AGAIN, SO MUCH :slight_smile:

Sorry, I'll do that now- thanks so much : D

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.