1

I have a data table whose column names are not known in advance

set.seed(1)
titles <- rep(letters[sample.int(3,replace = T)],2)
dt <- data.table( x = c(1, 1, 3, 1, 3, 1), y = c(1, 2, 1, 2, 1, 2) )
dt = dcast(dt, x+y ~ titles, fill=0, value.var = 'x')
> dt
   x y a b
1: 1 1 1 0
2: 1 2 1 2
3: 3 1 0 2

I wish to remove rows whose last column is 0, but 'subset' doesn't work. Why?

dt <- subset( dt, last(titles) > 0 )

I'd preferrably use data.table methods.

Amitai
  • 871
  • 1
  • 8
  • 21

1 Answers1

1

We can use get

 dt[get(last(titles))!=0]
akrun
  • 874,273
  • 37
  • 540
  • 662