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

Gerrit Eichner gerr|t@e|chner @end|ng |rom m@th@un|-g|e@@en@de
Fri May 24 08:13:02 CEST 2019


Hello, Jun,

try

split(df, f = factor(df$C, exclude = NULL))

For more info see ?factor, of course.

  Regards  --  Gerrit

---------------------------------------------------------------------
Dr. Gerrit Eichner                   Mathematical Institute, Room 212
gerrit.eichner using math.uni-giessen.de   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
http://www.uni-giessen.de/eichner
---------------------------------------------------------------------

Am 24.05.2019 um 06:40 schrieb Jun Shen:
> 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]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list