Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[R] write new SPSS data from R

35 views
Skip to first unread message

Lance Hoffmeyer

unread,
Nov 3, 2009, 11:50:47 AM11/3/09
to
Hey all,

Have been playing around with the R addon the past day or so.

The R WIki indicates one can write new SPSS data from R
http://wiki.r-project.org/rwiki/doku.php?id=tips:callingr:spss

Was wondering if anyone has a simple example of this?

So, I am crearing a new var in R called Var12.

begin program R.
data<-spssdata.GetDataFromSPSS()
data$Var12 <- (data$Var001 + data$Var002)
End program.

How would I get this new variable back into the existing SPSS
dataset?

Just trying to get familiar with how this addon works.

TIA,

Lance

JKPeck

unread,
Nov 3, 2009, 10:06:32 PM11/3/09
to
On Nov 3, 9:50 am, Lance Hoffmeyer <lance-n...@augustmail.com> wrote:
> Hey all,
>
> Have been playing around with the R addon the past day or so.
>
> The R WIki indicates one can write new SPSS data from Rhttp://wiki.r-project.org/rwiki/doku.php?id=tips:callingr:spss

>
> Was wondering if anyone has a simple example of this?
>
> So, I am crearing a new var in R called Var12.
>
> begin program R.
> data<-spssdata.GetDataFromSPSS()
> data$Var12 <- (data$Var001 + data$Var002)
> End program.
>
> How would I get this new variable back into the existing SPSS
> dataset?
>
> Just trying to get familiar with how this addon works.
>
> TIA,
>
> Lance

The process of writing back a dataset has two parts: first you create
a dictioary and transmit it to SPSS. Then you send a data frame.
Here is an example from the course I will teach next week post the
annual SPSS Directions conference in Las Vegas. It creates an SPSS
dataset containing the residuals from an R function. Error handling
is managed through the tryCatch function.

HTH,
Jon Peck

dict<- spssdictionary.CreateSPSSDictionary( c("caseNumber", "Case
Number", 0, "F8.0", "nominal"),
c("lmResiduals", model, 0, "F8.2", "scale"))

tryCatch({
spssdictionary.SetDictionaryToSPSS(residualsdataset, dict)
df = data.frame(res$residuals)
spssdata.SetDataToSPSS(residualsdataset,
data.frame(row.names(df), res$residuals))
},
error=function(e) {print(e)
cat("Failed to create residuals dataset. Dataset name must not
already exist: ",
residualsdataset)
}
)

0 new messages