1 year ago

#330582

test-img

Dan

R: How do I write more than 2 decimal places to a SPSS file (.sav)?

I want to write a correlation matrix to an SPSS file. The file format is .sav and the format of the required correaltion matrix is a mixture of character string columns and numeric columns (mostly numeric). SPSS automatically recognises strings as nominal data and numbers as scale data. If SPSS does not recognise the columns as nominal or scale then I cannot load the SPSS file directly into AMOS for some modelling. When I write a dataframe as a .sav file using the write.sav (df, "filename", digits = x) function from the misty package, SPSS will recognise the numbers as scale data but limits the decimal places to 2 and in the case of 0.999 SPSS rounds the value up to 1.00. I need SPSS to recognise my dataframe numbers to 5 or 8 decimal places. If anyone can help me solve this - I would be very grateful!

I´ve ensured that the dataframe column types are correct before writing the .sav file and I have tried converting the dataframe to a tibble and ensuring the columns are the correct class before writing the .sav file. In both cases, SPSS only loads the .sav files with two decimal points. I have checked the settings in SPSS and of course SPSS will display .sav files generated by itself to any number of deicmal points and .csv files. Writing a .csv file from R and then importing to SPSS is a work-around but sub-optimal as I need to write a workable file direct to the .sav format that SPSS and AMOS will read correctly to many deicmal points. Note that SPSS will display more deicmal places if the numeric values are written to the .sav file as strings, however, the .sav file will not then load into AMOS because the columns are recognised by SPSS as nominal data (strings) and not scale data (numbers). Ultimately, I need a .sav file that causes SPSS to recognise numeric data as scale data with more than 2 decimal places. AMOS just follows whatever SPSS does.

Some test code could be:

install.packages("misty")

library(misty)

col1 = c(0.111,0.222,0.333)

col2 = c(0.444,0.555,0.666)

col3 = c(0.777,0.888,0.999)

df = data.frame(col1, col2, col3)

df

str(df)

write.sav(df,"test.sav", digits = 5)

Thanks in advance!

r

decimal

spss

0 Answers

Your Answer

Accepted video resources