7

I have a data frame in R where one of the columns is gender. The values of gender are factors with "f" or "m" though if the data set is bad, it could be more (for instance NA).

I'm trying to split the data frame into a list of data frames with gender being unique. This way I can run the same models on the different populations.

Is there a better way then basically:

dfMale <- mydata[which(mydata$gender == "m"),]
dfFemale <- mdata[which(mydata$gender == "f"),]
dfOther <- mydata[!(1:dim(mydata][1] %in% c(which(mydata$gender == "m"),which(mydata$gender == "f"))]

Thanks.

3442
  • 8,248
  • 2
  • 19
  • 41
user1357015
  • 11,168
  • 22
  • 66
  • 111

1 Answers1

20
X<-split(df, df$gender)

from this question

Split data.frame based on levels of a factor into new data.frames

Community
  • 1
  • 1
DMT
  • 1,577
  • 10
  • 16