Dear R-helpers, I have a problem which I suppose is trivila, but... I have included NA values as factors ( (to be able to make nice printed summaries with NAs % ba category ) with the following code dat$x.f<-factor(dat$x, exclude=NULL); levels(dat$x.f)<-c("A1","A2","A3","A4","NA"); length(dat$x.f) Now, I want to impute the missing values. Is there a nice way to drop the NA factor instead of rewriting something of the sort dat$x.f<-factor(dat$x); levels(dat$x.f)<-c("A1","A2","A3","A4"); length(dat$x.f)? Thanks Anne ---------------------------------------------------- Anne Piotet Tel: +41 79 359 83 32 (mobile) Email: anne.piotet@m-td.com --------------------------------------------------- M-TD Modelling and Technology Development PSE-C CH-1015 Lausanne Switzerland Tel: +41 21 693 83 98 Fax: +41 21 646 41 33 -------------------------------------------------- [[alternative HTML version deleted]]
dat$x.f <- factor(replace(dat$x.f, dat$x.f == "NA", NA)) Anne wrote:> Dear R-helpers, > I have a problem which I suppose is trivila, but... > I have included NA values as factors ( (to be able to make nice printed summaries with NAs % ba category ) with the following code > > dat$x.f<-factor(dat$x, exclude=NULL); levels(dat$x.f)<-c("A1","A2","A3","A4","NA"); length(dat$x.f) > > Now, I want to impute the missing values. Is there a nice way to drop the NA factor instead of rewriting something of the sort > dat$x.f<-factor(dat$x); levels(dat$x.f)<-c("A1","A2","A3","A4"); length(dat$x.f)?-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894
Thank you! so easy and I did not think of it! ----- Original Message ----- From: "Chuck Cleland" <ccleland at optonline.net> To: "Anne" <anne.piotet at urbanet.ch> Cc: "R list" <r-help at stat.math.ethz.ch> Sent: Monday, December 06, 2004 2:12 PM Subject: Re: [R] removing NA as a level> dat$x.f <- factor(replace(dat$x.f, dat$x.f == "NA", NA)) > > Anne wrote: > > Dear R-helpers, > > I have a problem which I suppose is trivila, but... > > I have included NA values as factors ( (to be able to make nice printedsummaries with NAs % ba category ) with the following code> > > > dat$x.f<-factor(dat$x, exclude=NULL);levels(dat$x.f)<-c("A1","A2","A3","A4","NA"); length(dat$x.f)> > > > Now, I want to impute the missing values. Is there a nice way to dropthe NA factor instead of rewriting something of the sort> > dat$x.f<-factor(dat$x); levels(dat$x.f)<-c("A1","A2","A3","A4");length(dat$x.f)?> > -- > Chuck Cleland, Ph.D. > NDRI, Inc. > 71 West 23rd Street, 8th floor > New York, NY 10010 > tel: (212) 845-4495 (Tu, Th) > tel: (732) 452-1424 (M, W, F) > fax: (917) 438-0894 >
On 6 Dec 2004 at 13:58, Anne wrote:> Dear R-helpers, > I have a problem which I suppose is trivila, but... > I have included NA values as factors ( (to be able to make nice > printed summaries with NAs % ba category ) with the following code > > dat$x.f<-factor(dat$x, exclude=NULL); > levels(dat$x.f)<-c("A1","A2","A3","A4","NA"); length(dat$x.f) > > Now, I want to impute the missing values. Is there a nice way to drop > the NA factor instead of rewriting something of the sort > dat$x.f<-factor(dat$x); levels(dat$x.f)<-c("A1","A2","A3","A4"); > length(dat$x.f)?Hi Anne Is this dat$x.f[!is.na(dat$x.f), drop=T] what you want? ?"[.factor" Cheers Petr> > Thanks > > Anne > > > ---------------------------------------------------- > Anne Piotet > Tel: +41 79 359 83 32 (mobile) > Email: anne.piotet at m-td.com > --------------------------------------------------- > M-TD Modelling and Technology Development > PSE-C > CH-1015 Lausanne > Switzerland > Tel: +41 21 693 83 98 > Fax: +41 21 646 41 33 > -------------------------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz