Skip to content

removing factor values in the main data frame

8 messages · Ulrik Stervbo, hoda rahmati, David L Carlson +2 more

#
Hi all,I have the following main data frame:(mydata)?? ? ? ?$ TE : num 40 40 20 20 20 20 20 20 20 40 ...?? ? ? ?$ TR : num 49 49 28 28 28 28 28 28 28 49 ...?? ? ? ?$ COUNTRY : Factor w/ 27 levels "","AU","BA","BE",..: 8 8 8 8 8 ...among the COUNTRY I just need US and AU,first I get a subset to contain just these two countries:?? ? ? ?submydata=subset(mydata,COUNTRY%in%c("US","AU"))? ? ? ?factor(submydata$COUNTRY)?but after this when I get str of mydata again I have the same data frame with no changes in COUNTRY, however I want mydata to be like:?
? ? ? ?$ TE : num 40 40 20 20 20 20 20 20 20 40 ...?? ? ? ?$ TR : num 49 49 28 28 28 28 28 28 28 49 ...?? ? ? $ COUNTRY : Factor w/ 2 levels "","AU","US",..: 8 8 8 8 8 8 8 8 8 8Thanks for any help
#
Hi all,I have the following main data frame:(mydata)?? ? ? ?$ TE : num 40 40 20 20 20 20 20 20 20 40 ...?? ? ? ?$ TR : num 49 49 28 28 28 28 28 28 28 49 ...?? ? ? ?$ COUNTRY : Factor w/ 27 levels "","AU","BA","BE",..: 8 8 8 8 8 ...among the COUNTRY I just need US and AU,first I get a subset to contain just these two countries:?? ? ? ?submydata=subset(mydata,COUNTRY%in%c("US","AU"))? ? ? ?factor(submydata$COUNTRY)?but after this when I get str of mydata again I have the same data frame with no changes in COUNTRY, however I want mydata to be like:?
? ? ? ?$ TE : num 40 40 20 20 20 20 20 20 20 40 ...?? ? ? ?$ TR : num 49 49 28 28 28 28 28 28 28 49 ...?? ? ? ?$ COUNTRY : Factor w/ 2 levels "","AU","US",..: 8 8 8 8 8 8 8 8 8 8Thanks for any help
#
Hi,

I think droplevels(df) is what you are looking for.

Best wishes,
Ulrik

On Tue, 1 Mar 2016 at 17:33 hoda rahmati via R-help <r-help at r-project.org>
wrote:

  
  
#
I also tried droplevels but after getting str(submydata) again I see no changes in COUNTRY?
On Tuesday, March 1, 2016 8:38 AM, Ulrik Stervbo <ulrik.stervbo at gmail.com> wrote:
Hi,

I think droplevels(df) is what you are looking for.

Best wishes,
Ulrik
On Tue, 1 Mar 2016 at 17:33 hoda rahmati via R-help <r-help at r-project.org> wrote:
Hi all,I have the following main data frame:(mydata)?? ? ? ?$ TE : num 40 40 20 20 20 20 20 20 20 40 ...?? ? ? ?$ TR : num 49 49 28 28 28 28 28 28 28 49 ...?? ? ? ?$ COUNTRY : Factor w/ 27 levels "","AU","BA","BE",..: 8 8 8 8 8 ...among the COUNTRY I just need US and AU,first I get a subset to contain just these two countries:?? ? ? ?submydata=subset(mydata,COUNTRY%in%c("US","AU"))? ? ? ?factor(submydata$COUNTRY)?but after this when I get str of mydata again I have the same data frame with no changes in COUNTRY, however I want mydata to be like:?
? ? ? ?$ TE : num 40 40 20 20 20 20 20 20 20 40 ...?? ? ? ?$ TR : num 49 49 28 28 28 28 28 28 28 49 ...?? ? ? $ COUNTRY : Factor w/ 2 levels "","AU","US",..: 8 8 8 8 8 8 8 8 8 8Thanks for any help
? ? ? ? [[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.
#
Without more information it is hard to see where things go wrong.

To judge from your text, I am not sure you do things correct. You are doing
it along the lines of:
str(mydata)
submydata <- subset(mydata, ... )
submydata <- droplevels(submydata)
str(submydata)

right?
On Tue, 1 Mar 2016 at 17:45 hoda rahmati <hodarahmati68 at yahoo.com> wrote:

            

  
  
#
You need to learn how to send emails in plain text since html gets mangled on r-help. See your message below. If I understand your question, it has to do with what happens to factor levels when you subset your data. Subsetting a factor does not remove empty factor levels. This is documented on the manual page for subset():

"Factors may have empty levels after subsetting; unused levels are not automatically removed. See droplevels for a way to drop all unused levels from a data frame."

------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of hoda rahmati via R-help
Sent: Tuesday, March 1, 2016 10:32 AM
To: r-help at r-project.org
Subject: [R] Fw: removing factor values in the main data frame

Hi all,I have the following main data frame:(mydata)?? ? ? ?$ TE : num 40 40 20 20 20 20 20 20 20 40 ...?? ? ? ?$ TR : num 49 49 28 28 28 28 28 28 28 49 ...?? ? ? ?$ COUNTRY : Factor w/ 27 levels "","AU","BA","BE",..: 8 8 8 8 8 ...among the COUNTRY I just need US and AU,first I get a subset to contain just these two countries:?? ? ? ?submydata=subset(mydata,COUNTRY%in%c("US","AU"))? ? ? ?factor(submydata$COUNTRY)?but after this when I get str of mydata again I have the same data frame with no changes in COUNTRY, however I want mydata to be like:?
? ? ? ?$ TE : num 40 40 20 20 20 20 20 20 20 40 ...?? ? ? ?$ TR : num 49 49 28 28 28 28 28 28 28 49 ...?? ? ? ?$ COUNTRY : Factor w/ 2 levels "","AU","US",..: 8 8 8 8 8 8 8 8 8 8Thanks for any help

  

______________________________________________
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.
#
I advocate not converting to factor in the first place.  Delay that until you won't be wanting specific levels to be accounted for.
#
Also check ?"[.factor" and the drop=TRUE argument, which allows you to drop unused level from one particular factor. (Yes, there are good reasons that this is not the default.)

-pd