0

I have a traveler data set which as Origin countries with 200 levels and destination with same 200 levels.From this i have filtered out a single user specific records. Exaple:

 User   Origin    Destination
 A        Sing       AUS
 A        Sing       JAP
 A        Sing       US
 A        Sing       CHINA
 A        USA        Africa

from this i wrote a query to explore the variable values:

user$Origin

it displays as

[1]  Sing  USA
200 levels : AUS,AFR,AGN........USA,ZMB.

My question is

how to change the levels?

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
Leeya
  • 170
  • 2
  • 15
  • possible duplicate of [dropping factor levels in a subsetted data frame in R](http://stackoverflow.com/questions/1195826/dropping-factor-levels-in-a-subsetted-data-frame-in-r) – Rich Scriven Oct 17 '14 at 05:44

2 Answers2

0

You could use droplevels if you want to remove the levels that are not in the subset.

 traveler1 <- subset(traveler, User=="A")
 traveler1$Origin
 #[1] AGN AFR AUS AUS AFR
 #Levels: AFR AGN AUS Sing USA ZMB
  traveler2 <- droplevels(traveler1)
  traveler2$Origin
  #[1] AGN AFR AUS AUS AFR
  #Levels: AFR AGN AUS

data

 set.seed(24)
 traveler <- data.frame(User=sample(LETTERS[1:5], 25, replace=TRUE),
   Origin=sample(c("AUS", "AFR", "AGN", "Sing", "USA", "ZMB"), 25, replace=TRUE),
 Destination=sample(c("AUS", "AFR", "JAP", "US", "CHINA", "Africa"), 25, replace=TRUE))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Instead of dropping levels we can directly do factor of that variable again....

ie..,

factor(user$Origin)

[1] Sing USA

 2 Levels

it by default split the levels once again.

Thanks to all

Leeya
  • 170
  • 2
  • 15
  • If you have multiple columns, then you would need `traveler1[] <- lapply(traveler1, factor)` (based on my example) assuming that all the columns are factors. If you have some `numeric` columns as well, then `lapply(traveler1[sapply(traveler1, is.factor)], factor)`. By, using `droplevels`, you don't have to do this. – akrun Oct 17 '14 at 07:43