对于非常广泛的数据集,是否可以使用变量标签来选择列?
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"))
这不起作用,因为它会查找列名称。它可以查看标签吗?
编辑:我应该添加,除了将标签转换为列名称的明显作用之外。
我们可以得到attributes
“标签”,检查“里程”
library(dplyr)
library(stringr)
mtcars %>%
select(where(~ str_detect(attributes(.)$label, 'Miles')))
-output
# 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
# ..
或者使用base R
(with R 4.1.0
),用循环遍历列lapply
,提取labels
属性、用途grep
返回匹配的元素pattern
“英里”,获取names
并将其用于select
of subset
mtcars |>
lapply(\(x) attributes(x)$label) |>
grep(pattern = 'Miles', value = TRUE) |>
names() |>
{\(x) subset(mtcars, select = x)}()
-output
# 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
# ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)