Jun Shen
2019-May-24 04:40 UTC
[R] Fwd: How to retain the NA as a value for splitting a dataframe
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]]
Gerrit Eichner
2019-May-24 06:13 UTC
[R] Fwd: How to retain the NA as a value for splitting a dataframe
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 at 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 at 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. >