1

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?

lmo
  • 37,904
  • 9
  • 56
  • 69
  • 3
    Erhm...not clear, please provide a [Reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – digEmAll Apr 07 '17 at 10:26
  • Are you talking about DT package or `data.table`? – akrun Apr 07 '17 at 10:26
  • Can you rephrase you, please? – tagoma Apr 07 '17 at 10:30
  • Welcome to Stack Overflow. Please take a look at [how to ask good questions](http://stackoverflow.com/help/how-to-ask). You may also find this [R example making link helpful](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). People tend to use toy examples or existing datasets like iris for example. – micstr Apr 07 '17 at 10:36

1 Answers1

1

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 
josliber
  • 43,891
  • 12
  • 98
  • 133
micstr
  • 5,080
  • 8
  • 48
  • 76