如何使用 write_sav 编写唯一的变量名

How to write unique variable names with write_sav

我正在尝试编写一个 SPSS 文件,其中包含一些 haven_labelled 变量和从该变量创建的因子。使用几乎相同的变量名对我和我的用例来说很方便。我对 haven_labelled 变量使用了所有小写字母,对相应的因子变量使用了标题大小写。

当我用 write_sav 导出数据框时,SPSS 用 var1 记录标题大小写因子的变量名称,而不是标题大小写,在本例中为 Francophone .请注意,当我显着更改变量名称时,它会打印变量名称。

#This makes the data frame of haven labelled variable and a corresponding factor
test<-structure(list(francophone = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), labels = c(Francophone = 1, `Not Francophone` = 0), label = "Dichotomous variable, R is francophone", class = c("haven_labelled", "vctrs_vctr", "double")), Francophone = structure(c(1L, 1L, 1L,1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Not Francophone", "Francophone"), label = "Dichotomous variable, R is francophone", class = "factor")), row.names = c(NA,-10L), class = c("tbl_df", "tbl", "data.frame"))

#Create a second factor variable equivalent to the first factor, but with a different variable name
test$Franco<-test$Francophone
library(tidyverse)
library(haven)
#Write out the file; sorry I do not know how to use tmpfile() in this case. 
test %>% 
  write_sav(., path="~/Desktop/test2.sav")

要关闭循环,SPSS 中的变量名称必须是唯一的:https://www.ibm.com/docs/en/spss-statistics/version-missing?topic=list-variable-names

情况一直如此(并且可能不会改变)。