Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R使用列索引从带标签的数据框中提取属性(x)$label_R_Attributes_Label - Fatal编程技术网

R使用列索引从带标签的数据框中提取属性(x)$label

R使用列索引从带标签的数据框中提取属性(x)$label,r,attributes,label,R,Attributes,Label,我想从带标签的数据集中提取$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

我想从带标签的数据集中提取
$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 

因为数据集中有很多变量,我不使用SPSS,如果有人知道如何通过使用列索引而不是变量名来解决这个问题,那将非常有帮助。这可以使用
[[
提取器来完成,请参阅上的著名问题。
使用
帮助('read_spss')
中的示例进行测试

图书馆(避风港)
路径尝试
属性(df$gender)$`label`
attributes(df[,2])
#  $`names`
#[1] "gender"
#
#$row.names
#....
#
#$class
#
#[1] "tbl_df"     "tbl"        "data.frame"
library(haven)

path <- system.file("examples", "iris.sav", package = "haven")
df1 <- read_sav(path)
attributes(df1[[5]])$labels
#    setosa versicolor  virginica 
#         1          2          3