我正在使用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,数据是保密的。
1条答案
按热度按时间mccptt671#
看起来
base::attributes()
函数只能获取/设置vector的属性,而不是更改实际的单个属性。由于我们试图改变这个vector的
$labels
属性,并且该属性返回一个列表,因此您需要使用一个setter函数(如base::names()
)来修改属性$labels
返回的列表的名称。字符串