R通过使用列索引从避风港标记的数据帧中提取attribute(x)$ label

问题描述 投票:2回答:1

我想从标记为数据的避风港中提取$label属性,并且在使用列索引而不是列名时,它总是失败。

我做了什么:

library(haven)
df <- read_sav(mydata.sav)

attributes(df$gender) 
#$`label`
#[1] "Are you ...?"
#
#$format.spss
#[1] "F1.0"
#
#$display_width
#[1] 0
#
#$class
#[1] "haven_labelled"
#
#$labels
#
#female male
# 
#     1        2 

但使用:

attributes(df[,2])
#  $`names`
#[1] "gender"
#
#$row.names
#....
#
#$class
#
#[1] "tbl_df"     "tbl"        "data.frame"

因为数据集中有很多变量,而且我不使用SPSS,所以如果有人知道如何使用列索引而不是变量名来解决该问题,这将非常有帮助

r attributes label
1个回答
1
投票

这可以使用[[提取器完成,请参见Difference between [ and [[上的著名问题。使用[中的示例进行了测试。

[[
© www.soinside.com 2019 - 2024. All rights reserved.