Philip
Philip

Reputation: 99

Getting a specific value of data.table using a variable in R

I am wanting to us a specific value of a data.table. The column names and amount of columns are always changing, so I cannot use :

df$column_5[2]

Or :

df[2,5]

To get value I need.

I would like to do something similar to the following:

x <- 5
df[2,x]

But I get an error saying:

df[2,x] Error in [.data.table(df, 2, x) : j (the 2nd argument inside [...]) is a single symbol but column name 'x' is not found. Perhaps you intended DT[, ..x]. This difference to data.frame is deliberate and explained in FAQ 1.1."

Do you have any solutions to this issue?

Upvotes: 2

Views: 2579

Answers (1)

zack
zack

Reputation: 5405

Per @jogo's comment, you're using a data.table.

The solution is in the error:

Perhaps you intended DT[, ..x]

df[2, ..x] should do the trick.

Upvotes: 3

Related Questions