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

1hdlvixo  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(107)

我正在使用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,数据是保密的。

mccptt67

mccptt671#

看起来base::attributes()函数只能获取/设置vector的属性,而不是更改实际的单个属性。
由于我们试图改变这个vector的$labels属性,并且该属性返回一个列表,因此您需要使用一个setter函数(如base::names())来修改属性$labels返回的列表的名称。

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

字符串

相关问题