0

I have a data table with column headings as years, for example

head(gsdb_crossTab)

CONS_ID_URN     2010 2011 2012 2013 2014 2015 2016 2017
0TBX58            9   11   13   12   12   12   12    2
1                 2    4    1    0    0    0    0    0
1000000           0    0    1    0    1    0    0    0
1000007           1    0    0    0    1    1    0    0
1000009           0    0    0    0    1    0    0    0
1000010           0    1    1    0    1    0    0    1

class(gsdb_crossTab)
[1] "data.table" "data.frame"

If I try and remove rows in column 2010 with a value of zero with

dummy=gsdb_crossTab["2010">0]

then I still find dummy contains rows in column 2010 with rows equal containing zeros.

However: if I rename the column using

setnames(gsdb_crossTab, "2010", "FRED")

and then perform the filter

gsdb_crossTab <- gsdb_crossTab[FRED>0]

then I get the result I expect.

My question is: please could somebody suggest a way to perform this operation without renaming the column to FRED i.e. using the column label "2010"?

Thank you,

Phil,

Philip
  • 335
  • 3
  • 11
  • Use the `$` operator, if the colname is "2012" then it should work like this: gsdb_crossTab[gsdb_crossTab$`2012`>0,], make sure to add hyphens to the year, using `$` should automatically detect the variable though – timfaber May 10 '17 at 10:31
  • @timfaber, I think that will return `Error: unexpected numeric constant in "gsdb_crossTab $2010"` – amatsuo_net May 10 '17 at 10:39
  • hence the hyphen remark :) – timfaber May 10 '17 at 10:45

1 Answers1

0

Use back quote (I cannot put this in the comment as it uses grave accents)

dummy <- gsdb_crossTab[`2010` > 0]
amatsuo_net
  • 2,409
  • 11
  • 20