Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7820733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:24:38+00:00 2026-06-02T07:24:38+00:00

I’m working in R, but I need to deliver some data in SPSS format

  • 0

I’m working in R, but I need to deliver some data in SPSS format with both ‘variable labels’ and ‘value labels’ and I’m kinda stuck.

I’ve added variable labels to my data using the Hmisc‘s label function. This add the variable labels as a label attribute, which is handy when using describe() from the Hmisc package. The problem is that I cannot get the write.foreign() function, from the foreign package, to recognize these labels as variable labels. I imagine I need to modify write.foreign() to use the label attribute as variable label when writing the .sps file.

I looked at the R list and at stackoverflow, but I could only find a post from 2006 on the R list regarding exporting varibles labels to SPSS from R and it doesn’t seem to answer my question.

Here is my working example,

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

When I inspect the .sps file (see the content of ‘df.wf.sps’ below) my variable labels are identical to my variable names, except for foo that I renamed to “New crazy name for ‘foo’.” This variable has a new and seemly random name, but the correct variable label.

Does anyone know how to get the label attributes and the variable names exported as ‘variable labels’ and ‘labels names’ into a .sps file? Maybe there is a smarter way to store ‘variable labels’ then my current method?

Any help would be greatly appreciated.

Thanks, Eric

Content of ‘df.wf.sps’ export using write.foreign from the foreign package

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.

Update April 16 2012 at 15:54:24 PDT;

What I am looking for is a way to tweak write.foreign to write a .sps file where this part,

[…] 

VARIABLE LABELS
 id "id" 
 p.code "p.code" 
 p.label "p.label" 
 Nwcnf.f. "New crazy name for 'foo'" 

[…] 

looks like this,

[…] 

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'" 

[…]

The last line is a bit ambitious, I don’t really need to have a variables with white spaces in the names, but I would like the label attributes to be transferred to the .spas file (that I produce with R).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:24:42+00:00Added an answer on June 2, 2026 at 7:24 am

    Try this function and see if it works for you. If not, add a comment and I can see what I can do as far as troubleshooting goes.

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

    The above example is assuming that your data is named df, and you have used Hmisc to add labels, as you described in your question.

    Update: A Self-Contained Function

    If you do not want to alter your original file, as in the example above, and if you are connected to the internet while you are using this function, you can try this self-contained function:

    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)
    }
    

    Usage is simple:

    write.Hmisc.SPSS(df, "df.sav", "df.sps")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
I want to construct a data frame in an Rcpp function, but when I
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.