r语言 - 打印列名的选定范围



我发现如何在R上按列名打印选定的列,但不知道如何打印列名。

我的数据集叫t,我试过了:

print(colnames(t[3:ncol(t)]))

但是它不起作用。它打印所有的列。我可以通过删除colnames来打印选定的列,但它选择了列而不是列名。

简短回答

当使用data.table对象时,如果在[]中不使用逗号,似乎您不能隐式地引用列。

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 yourtobject

我不知道你是如何构建t对象的。我不得不从dput(head(t))的输出中删除.internal.selfref = < pointer:0x0000022fd86f1ef0 >,,以获得data.tabledata.frame类的R对象。这个对象在print(colnames(t[,3:ncol(t)]))下工作得很好(注意逗号)。我不使用data.table,所以我猜你需要使用逗号的语法,如果你想引用类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")
)

输出:

> 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"   
t <- mtcars
colnames(t[3:ncol(t)])
"disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"

最新更新