-1

I have a factor variable with a single column of data. It contains 33 levels, and as I understand each individual level has an integer value from 1-33. I was wondering how I could refer to that value rather than the levels label when subsetting a row?

Heres my attemp at writing the code for it:

Bexley <- subset(LE2016, borough[3])

I am trying to create a new object 'Bexley' containing only the level which was assigned the integer value of 3 from the dataframe 'LE2016'.

Thanks

LoriDori
  • 147
  • 2
  • 8
  • Could you put a reproducible example? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Edgar Santos Apr 19 '17 at 04:34
  • I'm not sure what are you asking for. Something like this? LE2016 <- data.frame(borough = factor(1:30)); library(dplyr); Bexley <- filter(LE2016, borough == 3) – Edgar Santos Apr 19 '17 at 04:41
  • Extract the levels of a factor in a variable: lev<-levels(factor_Variable) Then you can use this "lev" variable containing levels of your factor for subsetting. – tushaR Apr 19 '17 at 04:42

1 Answers1

0

You can use c or as.numeric to coerce the factor to integer and then do the subsetting. Here is a simple example:

library(dplyr)
DF <- data.frame(f=factor(c("a", "b", "c", "a", "b", "c"), levels=c("a", "b", "c")))
DF
  f
1 a
2 b
3 c
4 a
5 b
6 c
# filter out the 3th level
DF %>% filter(c(f) == 3)
  f
1 c
2 c

# c will coerce factor to integer
c(DF$f)
[1] 1 2 3 1 2 3
JasonWang
  • 2,414
  • 11
  • 12