对于非常广泛的数据集,是否可以使用变量标签来选择列?
library(expss)
data(mtcars)
mtcars = apply_labels(mtcars,
mpg = "Miles/(US) gallon",
cyl = "Number of cylinders",
disp = "Displacement (cu.in.)",
hp = "Gross horsepower",
drat = "Rear axle ratio",
wt = "Weight (1000 lbs)",
qsec = "1/4 mile time",
vs = "Engine",
vs = c("V-engine" = 0,
"Straight engine" = 1),
am = "Transmission",
am = c("Automatic" = 0,
"Manual"=1),
gear = "Number of forward gears",
carb = "Number of carburetors"
)
mtcars %>%
select(contains("Miles"))
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为它会查找列名称。它可以查看标签吗?
编辑:我应该添加,除了将标签转换为列名称的明显作用之外。
我们可以获得attributes“标签”,检查“里程”
library(dplyr)
library(stringr)
mtcars %>%
select(where(~ str_detect(attributes(.)$label, 'Miles')))
Run Code Online (Sandbox Code Playgroud)
-输出
# mpg
#Mazda RX4 21.0
#Mazda RX4 Wag 21.0
#Datsun 710 22.8
#Hornet 4 Drive 21.4
#Hornet Sportabout 18.7
#Valiant 18.1
#Duster 360 14.3
#Merc 240D 24.4
#Merc 230 22.8
#Merc 280 19.2
#Merc 280C 17.8
#Merc 450SE 16.4
# ..
Run Code Online (Sandbox Code Playgroud)
或者使用base R(with R 4.1.0),循环使用 的列lapply,提取labels属性,使用grep返回与 'Miles' 匹配的元素pattern,获取并在ofnames中使用selectsubset
mtcars |>
lapply(\(x) attributes(x)$label) |>
grep(pattern = 'Miles', value = TRUE) |>
names() |>
{\(x) subset(mtcars, select = x)}()
Run Code Online (Sandbox Code Playgroud)
-输出
# mpg
#Mazda RX4 21.0
#Mazda RX4 Wag 21.0
#Datsun 710 22.8
#Hornet 4 Drive 21.4
#Hornet Sportabout 18.7
#Valiant 18.1
#Duster 360 14.3
#Merc 240D 24.4
#Merc 230 22.8
#Merc 280 19.2
# ...
Run Code Online (Sandbox Code Playgroud)