Short answer
When using data.table
objects, it seems you cannot implicitely refer to columns if you do not use comma inside []
.
library(data.table)
# with data.frame
colnames(iris[3:ncol(iris)])
#> [1] "Petal.Length" "Petal.Width" "Species"
colnames(iris[,3:ncol(iris)])
#> [1] "Petal.Length" "Petal.Width" "Species"
# with data.table
t <- as.data.table(iris)
colnames(t[3:ncol(t)]) # here you get the colnames of the rows 3 to 5
#> [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
colnames(t[,3:ncol(t)])
#> [1] "Petal.Length" "Petal.Width" "Species"
For your t
object
I do not know how you have built the t
object. I had to remove .internal.selfref = < pointer:0x0000022fd86f1ef0 >,
from the output of dput(head(t))
, to get a R object of classes data.table
and data.frame
. This object works fine with print(colnames(t[,3:ncol(t)]))
(note the comma). I do not use data.table
so I guess you need to use the syntax with the comma if you want to refer to columns of an object of class data.table
.
library(data.table)
t <- structure(
list(
year = c(1949L, 1949L, 1949L, 1949L, 1949L, 1949L),
month = c(1L, 1L, 1L, 1L, 1L, 1L),
day = 1:6,
`Tmoy_AGEN-LA GARENNE` = c(6.9, 5.9, 2.9, 2, 7.9, 7.2),
Tmoy_AUXERRE = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
`Tmoy_BALE-MULHOUSE` = c(2.8, 1.3, 2.6, 1.6, 2.5, -0.7),
Tmoy_BESANCON = c(7.6, 2, 3.6, 3.1, 4, 3.3),
`Tmoy_BIARRITZ-PAYS-BASQUE` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
`Tmoy_BORDEAUX-MERIGNAC` = c(9.4, 5.5, 6.2, 5.5, 8.9, 3.5),
Tmoy_BOURGES = c(7.6, 4.2, 3.7, 3.1, 6.6, 6.2),
`Tmoy_BREST-GUIPAVAS` = c(5.5, 5, 5.5, 7.8, 8.3, 8.9),
`Tmoy_CHAMBERY-AIX` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
`Tmoy_CLERMONT-FD` = c(6.3, 3.8, 5.4, 0.9, 5.5, 5.3),
Tmoy_COGNAC = c(8.6, 4.8, 5.4, 5.3, 8.3, 5.6),
`Tmoy_DIJON-LONGVIC` = c(5.7, 3.8, 3.9, 0.3, 4.5, 4.6),
Tmoy_DINARD = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
`Tmoy_GRENOBLE-ST GEOIRS` = c(5.2, 5.3, -0.2, -0.9, 3.9, 1.2),
`Tmoy_LILLE-LESQUIN` = c(5.4, 3.7, 2.8, 3.8, 6.1, 5.5),
Tmoy_LUXEUIL = c(5.9, 1.6, 3.4, -0.6, 3, -1.5),
`Tmoy_LYON-BRON` = c(6.6, 5, 4.2, 1, 5.7, 4.1),
Tmoy_MARIGNANE = c(7.2, 12.3, 5.6, 3.2, 7, 6.3),
`Tmoy_METZ-FRESCATY` = c(4.6, 3.9, 2.6, 1, 4, 4.3),
Tmoy_MONTELIMAR = c(4.9, 9.8, 2.4, 4.6, 8.2, 7.6),
`Tmoy_NANTES-BOUGUENAIS` = c(8.5, 4.9, 4.6, 6.2, 8.2, 5.6),
Tmoy_NICE = c(7.6, 10.5, 7.2, 8.7, 7.6, 11.4),
`Tmoy_NIMES-COURBESSAC` = c(6.2, 10.7, 4.9, 5, 8.9, 10.1),
`Tmoy_PARIS-MONTSOURIS` = c(7.4, 4.4, 3.8, 3.6, 8.1, 6.4),
`Tmoy_PAU-UZEIN` = c(8.3, 6.1, 4.7, 4.8, 9.4, 7.3),
Tmoy_PERPIGNAN = c(8.1, 9.4, 6.5, 7.9, 9.6, 10.6),
`Tmoy_REIMS-PRUNAY` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
`Tmoy_ROUEN-BOOS` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_),
`Tmoy_ST ETIENNE-BOUTHEON` = c(7, 4.4, 3.2, 0.9, 7.3, 5.3),
`Tmoy_TOULOUSE-BLAGNAC` = c(5.8, 6, 2.5, 3.1, 7.7, 7.7),
Tmoy_TOURS = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)
),
row.names = c(NA, -6L),
class = c("data.table", "data.frame"),
sorted = c("year", "month", "day")
)
Output:
> colnames(t[,3:ncol(t)]) # note the addition of the comma
[1] "day" "Tmoy_AGEN-LA GARENNE" "Tmoy_AUXERRE"
[4] "Tmoy_BALE-MULHOUSE" "Tmoy_BESANCON" "Tmoy_BIARRITZ-PAYS-BASQUE"
[7] "Tmoy_BORDEAUX-MERIGNAC" "Tmoy_BOURGES" "Tmoy_BREST-GUIPAVAS"
[10] "Tmoy_CHAMBERY-AIX" "Tmoy_CLERMONT-FD" "Tmoy_COGNAC"
[13] "Tmoy_DIJON-LONGVIC" "Tmoy_DINARD" "Tmoy_GRENOBLE-ST GEOIRS"
[16] "Tmoy_LILLE-LESQUIN" "Tmoy_LUXEUIL" "Tmoy_LYON-BRON"
[19] "Tmoy_MARIGNANE" "Tmoy_METZ-FRESCATY" "Tmoy_MONTELIMAR"
[22] "Tmoy_NANTES-BOUGUENAIS" "Tmoy_NICE" "Tmoy_NIMES-COURBESSAC"
[25] "Tmoy_PARIS-MONTSOURIS" "Tmoy_PAU-UZEIN" "Tmoy_PERPIGNAN"
[28] "Tmoy_REIMS-PRUNAY" "Tmoy_ROUEN-BOOS" "Tmoy_ST ETIENNE-BOUTHEON"
[31] "Tmoy_TOULOUSE-BLAGNAC" "Tmoy_TOURS"