Eri*_*ail 7 export r labels spss
我在R工作,但我需要提供一些SPSS格式的数据,包括"变量标签"和"价值标签",我有点卡住了.
我已经添加变量标签使用我的数据Hmisc的label功能.这将变量标签添加为a label attribute,describe()从Hmisc包中使用时很方便.问题是我无法write.foreign()从foreign包中获得将这些标签识别为变量标签的功能.我想我需要修改write.foreign()以在编写文件时使用label attributeas .variable label.sps
我查看了R列表和stackoverflow,但我只能在R列表中找到2006年关于从R中将varibles标签导出到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"
Run Code Online (Sandbox Code Playgroud)
当我检查.sps文件时(参见下面'df.wf.sps'的内容),我和我variable labels的相同variable names,除了foo,我改名为"'foo'的新疯狂名字." 这个变量有一个新的,似乎是随机的名字,但是正确的variable label.
有谁知道如何将标签属性和变量名称导出为"变量标签"和"标签名称"到.sps文件中?也许有一种更智能的方法来存储'变量标签'然后我当前的方法?
任何帮助将不胜感激.
谢谢,埃里克
使用"df.wf.sps"出口内容write.foreign从foreign包
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.
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是一种调整write.foreign写入.sps这个部分的文件的方法,
[…]
VARIABLE LABELS
id "id"
p.code "p.code"
p.label "p.label"
Nwcnf.f. "New crazy name for 'foo'"
[…]
Run Code Online (Sandbox Code Playgroud)
看起来像这样,
[…]
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'"
[…]
Run Code Online (Sandbox Code Playgroud)
最后一行有点雄心勃勃,我真的不需要在名称中有一个带空格的变量,但我希望将标签属性转移到.spas文件(我用R生成).
尝试一下这个功能,看看它是否适合你。如果没有,请添加评论,我可以看看我能做些什么来排除故障。
# Step 1: Make a backup of your data, just in case
df.orig = df
# Step 2: Load the following function
get.var.labels = function(data) {
a = do.call(llist, data)
tempout = vector("list", length(a))
for (i in 1:length(a)) {
tempout[[i]] = label(a[[i]])
}
b = unlist(tempout)
structure(c(b), .Names = names(data))
}
# Step 3: Apply the variable.label attributes
attributes(df)$variable.labels = get.var.labels(df)
# Step 4: Load the write.SPSS function available from
# https://stat.ethz.ch/pipermail/r-help/2006-January/085941.html
# Step 5: Write your SPSS datafile and codefile
write.SPSS(df, "df.sav", "df.sps")
Run Code Online (Sandbox Code Playgroud)
上面的示例假设您的数据名为 name df,并且您已使用Hmisc添加标签,如您在问题中所述。
如果您不想更改原始文件(如上例所示),并且在使用此功能时已连接到互联网,则可以尝试以下自包含功能:
write.Hmisc.SPSS = function(data, datafile, codefile) {
a = do.call(llist, data)
tempout = vector("list", length(a))
for (i in 1:length(a)) {
tempout[[i]] = label(a[[i]])
}
b = unlist(tempout)
label.temp = structure(c(b), .Names = names(data))
attributes(data)$variable.labels = label.temp
source("http://dl.dropbox.com/u/2556524/R%20Functions/writeSPSS.R")
write.SPSS(data, datafile, codefile)
}
Run Code Online (Sandbox Code Playgroud)
用法很简单:
write.Hmisc.SPSS(df, "df.sav", "df.sps")
Run Code Online (Sandbox Code Playgroud)