首页 > 解决方案 > 在 R 中,我如何在属性中设置命名向量的名称

问题描述

我正在使用 SPSS 将数据读入havenR。其中一个变量的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,而且数据是机密的。

标签: rr-haven

解决方案


推荐阅读