Dear all, I get stuck when i try to export the data into SPSS format/file using "write.foreign()" Do you know how to do it exactly? What i have done is 1) First i type the following code in R: df<-data.frame(id,year,res1) names(df)<-c("idcode","year","resarrvl") write.foreign(df,"z:/daten/res.dat","z:/daten/res.sps",package="SPSS") 2) Then I run "res.sps" in SPSS DATA LIST FILE= "k:/res.dat" free (",") / idcode year resarrvl . VARIABLE LABELS idcode "idcode" year "year" resarrvl "resarrvl" . VALUE LABELS / idcode 1 "000CF7CC" 2 "000D0212" 3 "000D055F" ...... 1339 "06996BC5" 1340 "069972BF" . EXECUTE. 3) After running this file, there automatically appears a SPSS file. The variable name is correct, however, there is no data in that file. The character variable id has different "values" like 1.00 "000CF7CC", where 000CF7CC should be in the tab of data view instead of variable view. 4) Try to open "res.dat" It comes out the window "Text import wizard" The first column which should be the vairable id, shows only the "values" of id, i.e. 1, 2 and so on. And no real variables names. Thank you for any suggestion. Best wishes, He
On 12/07/2010 09:45 AM, He wrote:> Dear all, > > I get stuck when i try to export the data into SPSS format/file using > "write.foreign()" > Do you know how to do it exactly? > > What i have done is > 1) First i type the following code in R: > > df<-data.frame(id,year,res1) > names(df)<-c("idcode","year","resarrvl") > write.foreign(df,"z:/daten/res.dat","z:/daten/res.sps",package="SPSS") > > 2) Then I run "res.sps" in SPSS > > DATA LIST FILE= "k:/res.dat" free (",") > / idcode year resarrvl . > > VARIABLE LABELS > idcode "idcode" > year "year" > resarrvl "resarrvl" > . > > VALUE LABELS > / > idcode > 1 "000CF7CC" > 2 "000D0212" > 3 "000D055F" > ...... > > 1339 "06996BC5" > 1340 "069972BF" > . > > EXECUTE. > > 3) After running this file, there automatically appears a SPSS file. > The variable name is correct, however, there is no data in that file. > The character variable id has different "values" like 1.00 > "000CF7CC", where 000CF7CC should be in the tab of data view instead > of variable view. > > 4) Try to open "res.dat" > It comes out the window "Text import wizard" > The first column which should be the vairable id, shows only the > "values" of id, i.e. 1, 2 and so on. And no real variables names. >Hi He, Your data list statement looks like it is trying to read in a non-SPSS file (maybe a CSV file). Are you sure you want to do this? Jim
On Dec 7, 2010, at 09:19 , Jim Lemon wrote:> On 12/07/2010 09:45 AM, He wrote: >> Dear all, >> >> I get stuck when i try to export the data into SPSS format/file using >> "write.foreign()" >> Do you know how to do it exactly? >> >> What i have done is >> 1) First i type the following code in R: >> >> df<-data.frame(id,year,res1) >> names(df)<-c("idcode","year","resarrvl") >> write.foreign(df,"z:/daten/res.dat","z:/daten/res.sps",package="SPSS") >> >> 2) Then I run "res.sps" in SPSS >> >> DATA LIST FILE= "k:/res.dat" free (",") >> / idcode year resarrvl . >> >> VARIABLE LABELS >> idcode "idcode" >> year "year" >> resarrvl "resarrvl" >> . >> >> VALUE LABELS >> / >> idcode >> 1 "000CF7CC" >> 2 "000D0212" >> 3 "000D055F" >> ...... >> >> 1339 "06996BC5" >> 1340 "069972BF" >> . >> >> EXECUTE. >> >> 3) After running this file, there automatically appears a SPSS file. >> The variable name is correct, however, there is no data in that file. >> The character variable id has different "values" like 1.00 >> "000CF7CC", where 000CF7CC should be in the tab of data view instead >> of variable view. >> >> 4) Try to open "res.dat" >> It comes out the window "Text import wizard" >> The first column which should be the vairable id, shows only the >> "values" of id, i.e. 1, 2 and so on. And no real variables names. >> > Hi He, > Your data list statement looks like it is trying to read in a non-SPSS file (maybe a CSV file). Are you sure you want to do this? > > JimThat makes OK sense to me, the object is to _write_ an spss file in the end. write.foreign writes a plain text .dat file plus an spss "syntax file" which supposedly should read the file and generate the spss file. What looks odd to me is that res.dat appears to be written t z:/daten/ but read from k:, so I wonder whether spss is actually finding the same file? It is not particularly odd that a direct import of res.dat comes out without variable names and value labels, because that information is contained in res.sps. A closer look at k:res.dat (as a plain text file) and any messages from running res.sps could be illuminating. -- Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
On 12/07/2010 09:42 PM, He wrote:> Hi Jim, > > Thank you so much for your suggestion. I have tried ".csv" and ".sav". > ".sav" works better. I learned "DAT file" from the internet. There > were some similar discussion about wirte.foreign() before. > > I don't really know how the transfermation of the files between R and > SPSS works. When i got several files, I was a little lost. > > But I should emphasize my main problem which is the character variable > is not read shown up in the file. I mean i want to see e.g., 000CF7CC > in the data instead of its corresponding value 1 defined by SPSS > automatically. Another problem is how to keep the variable names in > the "data file". >Normally, when you write out a data file in SPSS format from R, it should correspond to an SPSS "system" or .SAV file. I would expect to see something like this: library(foreign) write.spss(df,file="k:/res.sav") Then in your SPSS file: GET /FILE="k:\res.sav". I don't think the .SAV extension is necessary, but I'm not sure. Jim