Reputation: 11
I have a data table with column names as strings of characters.
While
datatable[RowNumber, `Column Name`]
is working Fine,
datatable[RowNumber,datatable2[RowNumber,,ColumnName]]
is not working.
datatable2[RowNumber,,ColumnName2]= Column Name2
How do I fix this?
Upvotes: 1
Views: 72
Reputation: 5206
You might be running into a problem with needing to set "with = FALSE" if you are referencing a column name as a text string and not as is.
EXAMPLE
library(data.table)
head(iris)
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
# its a data.frame so convert to data.table
dt <- as.data.table(iris)
dt[1, Sepal.Length]
# get 5.1
dt[1, "Sepal.Length"]
# gives error, so you need with!
dt[1, "Sepal.Length", with = FALSE]
# get 5.1
# usually this is done when you code columns programmatically
my.col <- "Sepal.Length"
dt[1, my.col, with = FALSE]
# get 5.1
Upvotes: 1