1

I have a datatable with several numeric colum names, for instant 2020-4 and 2020-5. That I refer to as Nouvelle_periode = "2020-5" and Ancienne_periode = "2020-4" I manage to do things on these columns by using get as mentioned here.

But to delete a column it does not seem to work :

This works: Test_2 <- Test[,Revision:=get(Nouvelle_periode) - get(Ancienne_periode)]

But this does not : Test_3 <- Test_2 [,get(Ancienne_periode):=NULL] (I get the error object 2020-4 not found, even if it is still

And I have no idea why.

Here is a MWE :

library(data.table)
Nouvelle_periode = "2020-5"
Ancienne_periode = "2020-4"

Test <- data.table(Zone = c("Allemagne", "Allemagne", "Allemagne", 
  "Allemagne", "Espagne", "Espagne", "Espagne", "Espagne", "Etats-Unis", 
  "Etats-Unis", "Etats-Unis", "Etats-Unis", "France", "France", 
  "France", "France", "Italie", "Italie", "Italie", "Italie", "Japon", 
  "Japon", "Japon", "Japon", "Monde", "Monde", "Monde", "Monde", 
  "Royaume-Uni", "Royaume-Uni", "Royaume-Uni", "Royaume-Uni", "Zone euro", 
  "Zone euro", "Zone euro", "Zone euro"), Periode = c("2020", "2020", 
    "2021", "2021", "2020", "2020", "2021", "2021", "2020", "2020", 
    "2021", "2021", "2020", "2020", "2021", "2021", "2020", "2020", 
    "2021", "2021", "2020", "2020", "2021", "2021", "2020", "2020", 
    "2021", "2021", "2020", "2020", "2021", "2021", "2020", "2020", 
    "2021", "2021"), Grandeur = c("IPC", "PIB", "IPC", "PIB", "IPC", 
    "PIB", "IPC", "PIB", "IPC", "PIB", "IPC", "PIB", "IPC", "PIB", 
    "IPC", "PIB", "IPC", "PIB", "IPC", "PIB", "IPC", "PIB", "IPC", 
    "PIB", "IPC", "PIB", "IPC", "PIB", "IPC", "PIB", "IPC", "PIB", 
    "IPC", "PIB", "IPC", "PIB"), `2020-4` = c(0.7, -5, 1.4, 4.5, 
 -0.4, -5.7, 1.3, 5.2, 0.8, -4, 1.8, 3.9, 0.4, -5.4, 1.3, 5.1, 
 -0.2, -7.5, 0.6, 4.5, -0.1, -3.3, 0.2, 2.1, 2.1, -2.1, 2.4, 4.4, 
 1, -5.4, 1.6, 4.7, 0.4, -5.7, 1.3, 5.4), 
 `2020-5` = c(0.6, -6.3, 1.4, 5.2, -0.4, -9.1, 1, 6.7, 0.7, -5.4, 1.8, 4.3, 0.3, -8.2, 
1.2, 6.7, -0.2, -9.9, 0.6, 6.3, -0.4, -5.5, 0.1, 2.4, 2, -4.1, 2.3, 5.1, 1, -7.9, 1.4, 6.1, 0.3, -7.9, 1.1, 6.2))


Test_2 <- Test[,Revision:=get(Nouvelle_periode) - get(Ancienne_periode)]
Test_3 <- Test_2 [,get(Ancienne_periode):=NULL]
r2evans
  • 141,215
  • 6
  • 77
  • 149
Anthony Martin
  • 767
  • 1
  • 9
  • 28

1 Answers1

2

No need for get, use c(...) on the lhs of the := operator:

head( Test_2 [,c(Ancienne_periode) := NULL] )
#         Zone Periode Grandeur 2020-5 Revision
# 1: Allemagne    2020      IPC    0.6     -0.1
# 2: Allemagne    2020      PIB   -6.3     -1.3
# 3: Allemagne    2021      IPC    1.4      0.0
# 4: Allemagne    2021      PIB    5.2      0.7
# 5:   Espagne    2020      IPC   -0.4      0.0
# 6:   Espagne    2020      PIB   -9.1     -3.4
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • `c(...)` works indeed, where `get` did not. But for my previous line of code, it was the the opposite, `c()` does not work. Is there a reasonnably simple explanation ? – Anthony Martin Jun 11 '20 at 16:18
  • the rhs of `:=` is a little different in `data.table`, where it does not respect the `c(...)` distinction/shortcut. I think its use on the lfs was considered a convenience, but it is less clear how it should be used on the right. – r2evans Jun 11 '20 at 16:23
  • oops, "lhs" instead of 'lfs" ... and "less clear" how it could be implemented – r2evans Jun 11 '20 at 16:44
  • works without `c` as well. `()` for differentiating between NSE column name and character variable – chinsoon12 Jun 12 '20 at 02:45