私はRで作業していますが、「変数ラベル」と「値ラベル」の両方を使用してSPSS形式でデータを配信する必要があります。
Hmisc
のlabel
関数を使用して、データに変数ラベルを追加しました。これにより、変数ラベルがとして追加されます。これは、パッケージからlabel attribute
使用する場合に便利です。問題は、これらのラベルを変数ラベルとして認識する関数をパッケージから取得できないことです。ファイルを書き込むときにasを使用するように変更する必要があると思います。describe()
Hmisc
write.foreign()
foreign
write.foreign()
label attribute
variable label
.sps
Rリストとstackoverflowを調べましたが、RからSPSSへの変数ラベルのエクスポートに関するRリストの2006年の投稿しか見つかりませんでした。それは、私の質問に答えていないようです。
これが私の実例です、
# 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」の内容を参照)、「foo」の名前を「新しいクレイジーな名前」に変更したfooを除いて、variable labels
myと同じです。variable names
この変数の名前は新しく、一見ランダムに見えますが、正しい名前ですvariable label.
ラベル属性と変数名を「変数ラベル」と「ラベル名」として.sps
ファイルにエクスポートする方法を知っている人はいますか?たぶん、現在の方法よりも「変数ラベル」を保存するためのよりスマートな方法がありますか?
どんな助けでも大歓迎です。
ありがとう、エリック
パッケージwrite.foreign
から使用してエクスポートする「df.wf.sps」のコンテンツ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.
2012年4月16日15:54:24PDTに更新。
私が探しているのは、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で作成)に転送したいと思います。