how can I read spss data into R? - r

I have got a spss dataset which ends with .sps, not spss, now I'm trying to read it into R, I used read.table, but it doesn't work, read.spss doesn't work either. I'm thankful for any help!
I tried
read.spss("//Users//Weights//weights_merge.sps")
it says this file
"is not in any supported SPSS format"

.csv - format is suggested.
.csv is more flexible in R.
and in SPSS you can change raw data into .csv

Related

Problem with export data from excel to R (not interpreting data as numbers)

I have the following problem with exporting data from excel, and importing them into R.
My data in excel contains "," so excel interprets them as numbers.
But when I have this data in R I have "." so R interprets them as text instead of numbers.
For example,
In excel I have 12,765 and in R I have 12.765
Do you have any idea how can I fix this ?
I use the following code to import the data file into R:
library(openxlsx)
read.xlsx("pk.xlsx")
I suggest using the nice package readxl from the tidyverse (https://readxl.tidyverse.org/) :
library(readxl)
read_excel("pk.xlsx")
Your should find everything you need with the tidyverse to import different format of files (readr for all csv, tsv and so on... ).

Read .rdata without R

I have received an .rdata file with what I think should be a list of co-ordinates (x,y) and their corresponding gray value; however, I do not know exactly the data type/format within the .rdata file. Is there a way for me to read these data without R? I do not have access to R. I have Excel and Matlab on a Mac. Please help. Unfortunately, I am not allowed to pass on the data (my Googling tells me someone familiar with R can export the data into a text of csv file easily).
Thank you in advance.
I downloaded R, read the .rdata in using load() and saved it out again using write.csv().

Excel text data into R

I've been researching into this question, which I assume should be easy to fix, but am not having any luck. I have an excel file where each cell is some text of variable length. I'm wanting to read this into R so I can eventually do some text classification, but am failing. I get errors when using read.table and am struggling with all other alternatives. I've never worked with text data before so perhaps this is my issue. Having problems finding good examples of importing text data into R when it isn't in a corpus format.
There are special packages for reading data from the excel format. I mostly use readxl when I need to do this, but I know that there are several (a lot of them are described in this tutorial by datacamp, in the section Importing Excel files into R).
Another possibility (assuming you are using windows) is to copy the cells to the clipboard and use
read.table("clipboard")
for macOS and Linux there are similar commands, but I don't know them by heart.

Tab Delimited Data

Since I'm not able to find/decipher solutions to my problem I'll try asking.
Up until now I've only worked with other people's data (.csv files) in RStudio but this time around it's my own data, which I entered into Excel. All entries are tab-delimited, and I wouldn't enter data into Excel any other way. After some googling it seems like R and .xlsx files aren't best friends, so I saved my file in various other formats, .csv one of them. Thus I have tab-delimited .csv file.
The problem with loading tab-delimited .csv files also features here, but my problem is not with "reading some of the numeric variables in as factors" (whatever that means), but that data is loaded as semi-colon separated in R:
data <- read.table(file.choose(), sep="\t", header=T)
View(data)
Date.Miles.Time
2015-08-10;5;45
Apart from improper formatting there are 29 observations rather than 24; the last five entries are just ;;. Again, my problem is not the same as in the link but I figure there's no harm in trying Justin's suggestion in the answer, i.e. options(stringsAsFactors=FALSE) and then running the above again, but it achieves nothing.
read.csv() and read.delim() yield the same result. Any suggestions?

How to export a dataset to SPSS?

I want to export a dataset in the MASS package to SPSS for further investigation. I'm looking for the EuStockMarkets data set in the package.
As described in http://www.statmethods.net/input/exportingdata.html, I did:
library(foreign)
write.foreign(EuStockMarkets, "c:/mydata.txt", "c:/mydata.sps", package="SPSS")
I got a text file but the sps file is not a valid SPSS file. I'm really looking for a way to export the dataset to something that a SPSS can open.
As Thomas has mentioned in the comments, write.foreign doesn't generate native SPSS datafiles (.sav). What it does generate is the data in a comma delimited format (the .txt file) and a basic syntax file for reading that data into SPSS (the .sps file). The EuStockMarkets data object class is multivariate time series (mts) so when it's exported the metadata is lost and the resulting .sps file, lacking variable names, throws an error when you try to run it in SPSS. To get around this you can export it as a data frame instead:
write.foreign(as.data.frame(EuStockMarkets), "c:/mydata.txt", "c:/mydata.sps", package="SPSS")
Now you just need to open mydata.sps as a syntax file (NOT as a datafile) in SPSS and run it to read in the datafile.
Rather than exporting it, use the STATS GET R extension command. It will take a specified data frame from an R workspace/dataset and convert it into a Statistics dataset. You need the R Essentials for Statistics and the extension command, which are available via the SPSS Community site (www.ibm.com/developerworks/spssdevcentral)
I'm not trying to answer a question that has been answered. I just think there is something else to complement for other users looking for this.
On your SPSS window, you just need to find the first line of code and edit it. It should be something like this:
"file-name.txt"
You need to find the folder path where you're keeping your file:
"C:\Users\DELL\Google Drive\Folder-With-Your-File"
Then you just need to add this path to your file's name:
"C:\Users\DELL\Google Drive\Folder-With-Your-File\file-name.txt"
Otherwise SPSS will not recognize the .txt file.
Sorry if I'm repeating some information here, I just wanted to make it easier to understand.
I suppose that EuStockMarkets is a (labelled) data frame.
This should work and even keep the variable and value labels:
require(sjlabelled)
write_spss(EuStockMarkets, "mydata.sav")
Or you try rio:
rio::export(EuStockMarkets, "mydata.sav")

Resources