Dear All, I just want to remove ?NA? from the levels of a factor. For example: d<-data.frame(matrix(c("ww","ww","xx","yy","ww","yy","xx","yy","NA"), ncol=3, byrow=TRUE))> factor(d[, 3], exclude=NA)[1] xx yy NA Levels: NA xx yy But ?NA? is still listed in the levels. How can I solve this problem? Thanks in advance. Lisa -- View this message in context: http://r.789695.n4.nabble.com/Factor-function-tp3473984p3473984.html Sent from the R help mailing list archive at Nabble.com.
Hi Lisa, NA != "NA" The first represents a missing observation, the second represents a character string. HTH, Ista On Mon, Apr 25, 2011 at 3:53 PM, Lisa <lisajca at gmail.com> wrote:> Dear All, > > I just want to remove ?NA? from the levels of a factor. ?For example: > > d<-data.frame(matrix(c("ww","ww","xx","yy","ww","yy","xx","yy","NA"), > ncol=3, byrow=TRUE)) > >> factor(d[, 3], exclude=NA) > [1] xx yy NA > Levels: NA xx yy > > But ?NA? is still listed in the levels. How can I solve this problem? Thanks > in advance. > > Lisa > > > -- > View this message in context: http://r.789695.n4.nabble.com/Factor-function-tp3473984p3473984.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
Did you see the data frame "d"? Thanks. -- View this message in context: http://r.789695.n4.nabble.com/Factor-function-tp3473984p3474065.html Sent from the R help mailing list archive at Nabble.com.
Yes... did you understand that NA is not equal to "NA"? Best, Ista On Mon, Apr 25, 2011 at 4:31 PM, Lisa <lisajca at gmail.com> wrote:> Did you see the data frame "d"? Thanks. > > -- > View this message in context: http://r.789695.n4.nabble.com/Factor-function-tp3473984p3474065.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
Thank you for your reply again. I really know that NA is not "NA". I just want to figure out how to remove "NA" from the levels. Thanks again. -- View this message in context: http://r.789695.n4.nabble.com/Factor-function-tp3473984p3474127.html Sent from the R help mailing list archive at Nabble.com.
On Mon, Apr 25, 2011 at 12:53:40PM -0700, Lisa wrote:> Dear All, > > I just want to remove ?NA? from the levels of a factor. For example: > > d<-data.frame(matrix(c("ww","ww","xx","yy","ww","yy","xx","yy","NA"), > ncol=3, byrow=TRUE)) > > > factor(d[, 3], exclude=NA) > [1] xx yy NA > Levels: NA xx yy > > But ?NA? is still listed in the levels. How can I solve this problem?The column d[, 3] is already a factor. It is possible to avoid this using d<-data.frame(matrix(c("ww","ww","xx","yy","ww","yy","xx","yy","NA"), ncol=3, byrow=TRUE), stringsAsFactors=FALSE) Then, we get factor(d[, 3], exclude="NA") [1] xx yy <NA> Levels: xx yy Hope this helps. Petr Savicky.
Thank you for your help. Your R code works well. Lisa -- View this message in context: http://r.789695.n4.nabble.com/Factor-function-tp3473984p3474196.html Sent from the R help mailing list archive at Nabble.com.
Hi d<-data.frame(matrix(c("ww","ww","xx","yy","ww","yy","xx","yy","NA"), ncol=3, byrow=TRUE)) Change character value "NA" to missing value <NA> d[d[,3]=="NA",3]<-NA If you want drop any unused levels of a factor just use factor(d[,3]) [1] xx yy <NA> Levels: xx yy Regards Petr r-help-bounces at r-project.org napsal dne 25.04.2011 23:03:15:> Thank you for your reply again. I really know that NA is not "NA". Ijust> want to figure out how to remove "NA" from the levels. Thanks again. > > -- > View this message in context:http://r.789695.n4.nabble.com/Factor-function-> tp3473984p3474127.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Thank you so much! Lisa -- View this message in context: http://r.789695.n4.nabble.com/Factor-function-tp3473984p3475549.html Sent from the R help mailing list archive at Nabble.com.
On Tue, Apr 26, 2011 at 10:51:33AM +0200, Petr PIKAL wrote:> Hi > > > d<-data.frame(matrix(c("ww","ww","xx","yy","ww","yy","xx","yy","NA"), > ncol=3, byrow=TRUE)) > > Change character value "NA" to missing value <NA> > d[d[,3]=="NA",3]<-NA > > If you want drop any unused levels of a factor just use > > factor(d[,3]) > [1] xx yy <NA> > Levels: xx yyAn explicit NA is a good idea. If the NA is introduced before creating the data frame, then also the data frame will not contain the unwanted level. a<-matrix(c("ww","ww","xx","yy","ww","yy","xx","yy","NA"), ncol=3, byrow=TRUE) a[a[,3]=="NA",3]<-NA d<-data.frame(a) d[,3] [1] xx yy <NA> Levels: xx yy If the replacement should be done in the whole matrix, then a[a=="NA"]<-NA may be used. Petr Savicky.