在R中,我如何在属性中设置命名向量的名称

  • 本文关键字:设置 向量 属性 r r-haven
  • 更新时间 :
  • 英文 :


我正在使用haven从SPSS读取数据到R。其中一个变量的labels属性非常长:

> attributes(source$VAX8_behavior)
$label
[1] "Which of the following applies to you regarding the COVID-19 vaccine? [8M]"
$format.spss
[1] "F3.0"
$display_width
[1] 7
$class
[1] "haven_labelled" "vctrs_vctr"     "double"        
$labels
                                 Decline to answer 
                                              -999 
                                    Item not shown 
                                              -888 
                                  Data entry error 
                                              -777 
                            Failed attention check 
                                              -666 
                                 Lost to follow-up 
                                              -555 
                                  Cannot Calculate 
                                              -444 
     I am not planning to get the COVID-19 vaccine 
                                                 1 
     I am considering getting the COVID-19 vaccine 
                                                 2 
I am actively planning to get the COVID-19 vaccine (e.g., I have scheduled an appointment to get the vaccine or plan to 
                                                 3 

所以,我想改变这个:

attr(source$VAX8_behavior, "labels")[9]
I am actively planning to get the COVID-19 vaccine (e.g., I have scheduled an appointment to get the vaccine or plan to 

如何将其更改为"…我已经安排了一个约会……?也就是说,在一个属性的命名向量上更改名称的语法是什么?

这行不通:

attr(source$VAX8_behavior, "labels")[9] <- 
"... I have scheduled an appointment ..."

因为它设置了值而不是标签:

$labels
                                 Decline to answer 
                                            "-999" 
                                    Item not shown 
                                            "-888" 
                                  Data entry error 
                                            "-777" 
                            Failed attention check 
                                            "-666" 
                                 Lost to follow-up 
                                            "-555" 
                                  Cannot Calculate 
                                            "-444" 
     I am not planning to get the COVID-19 vaccine 
                                               "1" 
     I am considering getting the COVID-19 vaccine 
                                               "2" 
I am actively planning to get the COVID-19 vaccine (e.g., I have scheduled an appointment to get the vaccine or plan to 
         "... I have scheduled an appointment ..." 

我猜解决方案需要setNames()mostattributes()

很抱歉我不能提供ReprEx,因为我没有SPSS,数据是保密的。

似乎base::attributes()函数只能获取/设置矢量的属性,而不能改变实际的单个属性。

由于我们试图改变这个向量的$labels属性,并且该属性返回一个列表,因此您需要使用像base::names()这样的setter函数来修改属性$labels返回的列表的名称。

names(attributes(source$VAX8_behavior, "labels"))[9] = "... I have scheduled an appointment ..."