0

I have a dataset called "x" that produces the following records when I do head(x, 1)

 VRTG_ID_NR EEG_VRTG_CAT_V GEBR_IDENT_KEUR PL_KEURING NAAM_VRTG_AANB DAT_RESULT_KEUR TYD_RESULT_KEUR KL_CODE_EU_1 KL_CODE_EU_2
 1 VF1JA04N522215749             M1            NULL       NULL           NULL        20090527             906            6         NULL
 RES_CODE_KEUR KENT_LAND_OORS LAND_HERK YEAR 
 1           GDK      ME-QT 761         D 2009

I get the following when I show the classes of the relevant column

$YEAR [1] "numeric"

I now want to create a subset where I only see data from the years 2009 en 2010. So I tried

 x_subset <- x[x$YEAR >=2009 & <= 2011]

That however gives me the following error:

data frame with 0 columns and 992287 rows

While actually I want an overview with a subset of the records between 2009 and 2011...

user3706202
  • 197
  • 1
  • 3
  • 14
  • 1
    You want x[x$YEAR >=2009 & x$YEAR <= 2011] – Serban Tanasa Jun 19 '15 at 15:20
  • 4
    What is `class(x$YEAR)`? Does that field contain non-numeric values? You really should try to create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) when asking for help to make it clear what's going on. Copying what's shown on screen isn't as helpful as posting a `dupt()` (as described in the link) – MrFlick Jun 19 '15 at 15:22
  • Presumably `x_subset <- x[as.numeric(x$YEAR) >= 2009 & as.numeric(x$YEAR)<= 2011]` will give you the desired result. – RHertel Jun 19 '15 at 15:46
  • 1
    @RHertel `as.numeric()` on a factor probably doesn't return what you think it will in this case (`as.numeric(factor(2009))==1`). You would need `as.numeric(as.character(...))` but better to figure out why it's a factor in the first place. – MrFlick Jun 19 '15 at 15:52
  • Ah, yes - I remember that I had to do such constructions of the type `as.numeric(as.character(..` in similar cases. I fully agree that `class(x$YEAR)` would be helpful. Please ignore my comment above. – RHertel Jun 19 '15 at 16:00
  • @RHertel http://stackoverflow.com/a/3418192/2954547 – shadowtalker Jun 19 '15 at 17:28
  • Thanks for the interesting and helpful link, @ssdecontrol – RHertel Jun 19 '15 at 18:51
  • Guys, thanks for your input. I concerted my x$YEAR to numeric values but still does not get what I want... See my edits above. Any further thoughts? – user3706202 Jun 21 '15 at 17:15

1 Answers1

-1

If YEAR is a factor variable, first convert it to a numeric:

x$YEAR <- as.numeric(x$YEAR)

I think you are missing a comma:

x_subset <- x[x$YEAR >=2009 & x$YEAR <= 2011,]
  • This won't work. Try `x <- as.factor(c('2009', '2010')); as.numeric(x)`. See also http://stackoverflow.com/a/3418192/2954547 – shadowtalker Jun 19 '15 at 17:26
  • If it's a factor and the first line is replaced with `x$YEAR <- as.numeric(levels(x$YEAR))` it might work. – RHertel Jun 19 '15 at 18:32
  • @RHertel you need to do `factor2numeric <- function (x) as.numeric(levels(x))[x]; x$YEAR <- factor2numeric(x$YEAR)`, as per the link I posted. – shadowtalker Jun 19 '15 at 19:13