[R] Fwd: How to retain the NA as a value for splitting a dataframe

Jun Shen jun@@hen@ut @end|ng |rom gm@||@com
Fri May 24 06:40:55 CEST 2019


Dear list,

Say I have a data frame with NA in the variable which I want to use as a
sorting variable for splitting the data frame.

df <- data.frame(A=1:10, B=c(rep(99,5), rep(100,5)), C=c(rep(NA,3),
rep(1,3), rep(2,4)))

split(df, f=df[c('C')], drop=FALSE), I got the output as follows. I was
hoping to retain the part of the df where C=NA. drop=FALSE doesn't seem to
take effect here. Appreciate any comments. Thanks.

$`1`
  A   B C
4 4  99 1
5 5  99 1
6 6 100 1

$`2`
    A   B C
7   7 100 2
8   8 100 2
9   9 100 2
10 10 100 2


Jun

	[[alternative HTML version deleted]]



More information about the R-help mailing list