0

I seem to have problems using column names within data.table that have certain characters, including currency symbols and numeric characters?

Are there any sensible ways of working with these, e.g. referring to such column names by reference? Here's an example:

e <- data.table(a = 1:5, b = 2:6, Revenues_(£000) = 3:7)
e[,Tax_(£000) = a + Revenues_(£000),]

Many Thanks in advance

Ben Watts
  • 53
  • 5
  • Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Apr 20 '16 at 15:43
  • Please update your question. Don't put additional info in the comments. And it is also wise to specify what problems you run into. – Jaap Apr 20 '16 at 15:49

1 Answers1

4

[Updated based on new example]

Backticks are what you are looking for, I believe:

e <- data.table(a = 1:5, b = 2:6, `Revenues_(£000)` = 3:7)

e[, .(`Tax_(£000)` = a + `Revenues_(£000)`)]

Or, if you want to add the sum as a new column to e, what you are looking for is:

e[, `Tax_(£000)` := a + `Revenues_(£000)`]
eddi
  • 49,088
  • 6
  • 104
  • 155
  • not sure that's really what I'm looking for, thanks for trying - I have updated the question with an extra line of code to try and make it more clear to you – Ben Watts Apr 20 '16 at 15:54
  • 1
    `e$col <- ...` is not efficient way to add new columns, use `e[, col := ...]` operator instead – jangorecki Apr 20 '16 at 16:29