小编rol*_*and的帖子

R使用列索引从避风港标记数据框中提取属性(x)$标签

我想$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 
Run Code Online (Sandbox Code Playgroud)

但使用:

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

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

attributes label r

7
推荐指数
1
解决办法
2984
查看次数

标签 统计

attributes ×1

label ×1

r ×1