1
votes

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?

1
You are using a data.table (i.e. not a dataframe)! Do you want a data.table-operation or a dataframe-operation? - jogo
Try df[2, x, with = FALSE] - Mako212
@jogo My apologies for using the incorrect word. Do you have a solution to my question? Keep in mine I am still relatively new to R. - Philip
@Mako212 Thank you! That was exactly what I needed! - Philip
When you're working with the data.table library, it changes how brackets behave with objects of class data.table. If you want to take advantage of R's standard data.frame evaluation of brackets, you need to specify with = F - Mako212

1 Answers

2
votes

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.