1

I've got a dataframe that I need to split based on the values in one of the columns - most of them are either 0 or 1, but a couple are NA, which I can't get to form a subset. This is what I've done:

all <- read.csv("XXX.csv")
splitted <- split(all, all$case_con)

dim(splitted[[1]]) #--> gives me 185
dim(splitted[[2]]) #--> gives me 180

but all contained 403 rows, which means that 38 NA values were left out and I don't know how to form a similar subset to the ones above with them. Any suggestions?

Jaap
  • 81,064
  • 34
  • 182
  • 193
julia
  • 23
  • 3
  • 2
    It is better to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). That makes it a lot easier for other people to help you. – Jaap Feb 05 '17 at 10:57
  • Hacky soln would just be to replace the NAs with a dummy value before splitting – Akhil Nair Feb 05 '17 at 11:33
  • Can you run `sapply(splitted, dim)`? This will tell us if you have any NAs in your `case_con` variable. – Roman Luštrik Feb 05 '17 at 11:44
  • @RomanLuštrik `sapply(splitted,dim) 0 1 [1,] 185 180 [2,] 2273 2273` but `dim(all) #--> [1] 403 2273` So I'm losing the remaining values that are neither in splitted[[1]] nor in splitted[[2]] – julia Feb 05 '17 at 12:06
  • @julia please edit this information into your question. – Roman Luštrik Feb 05 '17 at 13:26

2 Answers2

0

Try this:

splitted<-c(split(all, all$case_con,list(subset(all, is.na(case_con))))

This should tack on the data frame subset with the NAs as the last one in the list...

MPhD
  • 456
  • 2
  • 9
0
list(split(all, all$cases_con), split(all, is.na(all$cases_con)))

I think it would be work. Ty

유준상
  • 23
  • 3