Semicolons appeared in CSV file when upload into R - r

I was instructed by the book Analyzing Financial Data and Implementing Financial Models Using R (Clifford S. Ang 2015, chapter 8) to download the USA real GDP data from IMF website https://www.imf.org/en/home. The downloaded file is a .xls, and I converted it to .csv under the name "USRGDP IMF WEO.csv" as instructed.
Then, when I ran the codes on R, the numeric data in the .csv file reversed itself, with semicolons appeared. Illustration is as below:
The original file's number format (when opened by Excel):
The code:
library(quantmod)
us.rgdp <- read.csv("USRGDP IMF WEO.csv", header = FALSE)
The output:
What can be done to fix the data? Thank you, from a beginner to R.

I usually have much more consistent and speedier results using data.table::fread() function. It works for zipped files too.

Related

How to read macro enabled excel files in R?

I have 2 excel files which have macros in it. The file extension ends with .xlsb and .xlsm. I want to read these files into R and do exactly what excel is doing with these files in terms of data inputs in R. What is the way to go about it?
For example: if the excel file calculates house prices in sheet 2 based on data input in sheet 1, how can the same results for house price calculation be obtained in R?
You might take a look at the R package RDCOMClient:
https://github.com/omegahat/RDCOMClient
Here is a nice example shown:
https://www.r-bloggers.com/2021/07/rdcomclient-read-and-write-excel-and-call-vba-macro-in-r/

Why Unicodes get converting from excel file to r data frame

I have excel file with unicode characters (like Korean and Japan language), when i load it from excel file to r data frame it's converting to some codes. For example
Excel Source Column
values=KR|512207456|투비씨엔씨(주)
After load the excel file to
DF = KR|512207456|<U+D22C><U+BE44><U+C528><U+C5D4><U+C528>(<U+C8FC>)
I did lot of google to find the solution, unfortunately not able to find any solution. Any help would be highly appreciated.

Time series data homogenization using climatol package in R - .dah file missing

I am trying to homogenize rainfall time series data for 12 stations in R (RStudio) using homogen tool in climatol package. I used monthly total series computed using dd2m tool. The homogen command runs well and also generates the results including .rda and .pdf files. But I can't see the .dah (homogenized data with missing data filled) and .esh files being created in working folder as expected.
Any help on what might have happen, and how can I get this result would be appreciated.
Cheers
I just figured out that we can export the 'would be' content of the dah file by loading the rda content to R and then writing to a text file, i.e.
load('rTest_1950-2000.rda')
write.csv(dah,"C:/Test/Test-dah.csv").

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