我在 R 中工作,但我需要以 SPSS 格式提供一些带有“变量标签”和“值标签”的数据,但我有点卡住了。
Hmisc
我使用'slabel
函数为我的数据添加了变量标签。这会将变量标签添加为 a ,这在从包中label attribute
使用时很方便。问题是我无法从包中获得将这些标签识别为变量标签的功能。我想我需要修改以在编写文件时使用as 。describe()
Hmisc
write.foreign()
foreign
write.foreign()
label attribute
variable label
.sps
我查看了 R 列表和 stackoverflow,但我只能在R 列表中找到 2006 年关于将变量标签从 R 导出到 SPSS 的帖子,它似乎没有回答我的问题。
这是我的工作示例,
# First I create a dummy dataset
df <- data.frame(id = c(1:6), p.code = c(1, 5, 4, NA, 0, 5),
p.label = c('Optometrists', 'Nurses', 'Financial analysts',
'<NA>', '0', 'Nurses'), foo = LETTERS[1:6])
# Second, I add some variable labels using label from the Hmisc package
# install.packages('Hmisc', dependencies = TRUE)
library(Hmisc)
label(df) <- "Sweet sweet data"
label(df$id) <- "id !@#$%^"
label(df$p.label) <- "Profession with human readable information"
label(df$p.code) <- "Profession code"
label(df$foo) <- "Variable label for variable x.var"
# modify the name of one varibes, just to see what happens when exported.
names(df)[4] <- "New crazy name for 'foo'"
# Third I export the data with write.foreign from the foreign package
# install.packages('foreign', dependencies = TRUE)
setwd('C:\\temp')
library(foreign)
write.foreign(df,"df.wf.txt","df.wf.sps", package="SPSS")
list.files()
[1] "df.wf.sps" "df.wf.txt"
当我检查.sps
文件时(请参阅下面 'df.wf.sps' 的内容), myvariable labels
与 my 相同variable names
,除了 foo 我重命名为“'foo' 的新疯狂名称”。这个变量有一个新的看似随机的名字,但是正确的variable label.
有谁知道如何将标签属性和变量名称导出为“变量标签”和“标签名称”到.sps
文件中?也许有比我目前的方法更聪明的方法来存储“变量标签”?
任何帮助将不胜感激。
谢谢,埃里克
write.foreign
使用从foreign
包中导出的“df.wf.sps”的内容
DATA LIST FILE= "df.wf.txt" free (",")
/ id p.code p.label Nwcnf.f. .
VARIABLE LABELS
id "id"
p.code "p.code"
p.label "p.label"
Nwcnf.f. "New crazy name for 'foo'"
.
VALUE LABELS
/
p.label
1 "0"
2 "Financial analysts"
3 "Nurses"
4 "Optometrists"
/
Nwcnf.f.
1 "A"
2 "B"
3 "C"
4 "D"
5 "E"
6 "F"
.
EXECUTE.
2012 年 4 月 16 日太平洋夏令时间 15:54:24 更新;
我正在寻找的是一种调整方法write.foreign
来编写一个.sps
文件,其中这部分,
[…]
VARIABLE LABELS
id "id"
p.code "p.code"
p.label "p.label"
Nwcnf.f. "New crazy name for 'foo'"
[…]
看起来像这样
[…]
VARIABLE LABELS
id "id !@#$%^"
p.code "Profession code"
p.label "Profession with human readable information"
"New crazy name for 'foo'" "New crazy name for 'foo'"
[…]
最后一行有点雄心勃勃,我真的不需要名称中包含空格的变量,但我希望将标签属性传输到 .spas 文件(我使用 R 生成的文件)。