7

私はRで作業していますが、「変数ラベル」と「値ラベル」の両方を使用してSPSS形式でデータを配信する必要があります。

Hmisclabel関数を使用して、データに変数ラベルを追加しました。これにより、変数ラベルがとして追加されます。これは、パッケージからlabel attribute使用する場合に便利です。問題は、これらのラベルを変数ラベルとして認識する関数をパッケージから取得できないことです。ファイルを書き込むときにasを使用するように変更する必要があると思います。describe()Hmiscwrite.foreign()foreignwrite.foreign()label attributevariable 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 labelsmyと同じです。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で作成)に転送したいと思います。

4

2 に答える 2

4

この機能を試して、うまくいくかどうかを確認してください。そうでない場合は、コメントを追加すると、トラブルシューティングに関する限り、何ができるかがわかります。

# 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")

上記の例は、データに名前が付けられており、質問で説明したように、ラベルを追加するために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)
}

使い方は簡単です:

write.Hmisc.SPSS(df, "df.sav", "df.sps")
于 2012-04-21T17:57:17.707 に答える
1

リンクした関数(ここ)は機能するはずですが、問題は、データセットにSPSSスクリプトファイルの作成に必要な属性variable.labelと属性が実際にないことだと思います。label.table

SPSSにアクセスできませんが、次のことを試して、少なくとも正しい方向を示しているかどうかを確認してください。dput残念ながら、手動で出力を編集する以外に、これを行う簡単な方法はわかりません。

df = structure(list(id = 1:6, 
               p.code = c(1, 5, 4, NA, 0, 5), 
               p.label = structure(c(5L, 4L, 2L, 3L, 1L, 4L), 
                                   .Label = c("0", "Financial analysts",
                                              "<NA>", "Nurses", 
                                              "Optometrists"), 
                                   class = "factor"), 
               foo = structure(1:6, 
                               .Label = c("A", "B", "C", "D", "E", "F"), 
                               class = "factor")), 
               .Names = c("id", "p.code", "p.label", "foo"),
          label.table = structure(list(id = NULL,
                             p.code = NULL,
                             p.label = structure(c("1", "2", "3", "4", "5"),
                                      .Names = c("0", "Financial analysts", 
                                                 "<NA>", "Nurses", 
                                                 "Optometrists")),
                             foo = structure(1:6, 
                                  .Names = c("A", "B", "C", "D", "E", "F"))),
                             .Names = c("id", "p.code", "p.label", "foo")),
          variable.labels = structure(c("id !@#$%^",  "Profession code", 
                                 "Profession with human readable information",
                                 "New crazy name for 'foo'"), 
                            .Names = c("id", "p.code", "p.label", "foo")), 
          codepage = 65001L)

dput上記をサンプルデータセットのの出力と比較してください。label.tablevariable.labelsが追加され、のような行row.names = c(NA, -6L), class = "data.frame"が削除されていることに注意してください。

アップデート

注:これはwrite.foreignRのデフォルト関数では機能しません。これをテストするには、最初にここwrite.SPSSで共有されている関数をロードする必要があります。もちろん、パッケージがロードされていることを確認してください。次に、次のようにファイルを書き込みます。foreign

write.SPSS(df, datafile="df.sav", codefile="df.sps")
于 2012-04-17T05:39:04.977 に答える