Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split data frame into two by column value [duplicate]

Tags:

dataframe

r

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.

like image 777
user1357015 Avatar asked Dec 02 '22 18:12

user1357015


1 Answers

X<-split(df, df$gender)

from this question

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

like image 186
DMT Avatar answered Dec 22 '22 22:12

DMT