Downloading CSV_GDX_tools.exe package - r

I have to work with GAMS and R to extract data however I am a new R user and never have used GAMS before. I need to download a package called CSV_GDX_tools.exe and I have no idea what that is...
When I try to install it in R, I get this error message:
Warning in install.packages :
package ‘CSV_GDX_tools.exe’ is not available (for R version 3.3.2)
Can anyone please help me how and where I can download the package?

First, that does not sound like an R-package, but rather like an outdated utility program for GAMS.
I say outdated, because GAMS now has built-in the functionality to convert GDX (GAMS data files) to CSV files, which can be read by any statistics program including R.
GAMS also gives you the option of exporting your data to an SQLite database file (.db), which can be read by R.
Have a look here:
https://www.gams.com/help/index.jsp?topic=%2Fgams.doc%2Fuserguides%2Fmccarl%2Fgdx_utilities.htm

Related

How to load .Rdata from an R package?

I am developing an R package. In the package, I stored some .Rdata file from the simulation results. Now I would like to load/use those .Rdata in the rmarkdown so that I can write vignettes. I can load those data from my computer. But my question is how do I load data from my own package directory? Because in the future some users of the package may need to follow the same code from the vignette and reproduce the results or plots.
Assuming you used devtools::use_data(), you can use library(YOUR_PACKAGE); data("dataset") in your vignette, where "dataset" is the data you're talking about. Running devtools::load_all() will make the data available to you as you are writing the vignette.

Can I load a package's data set without installing the package?

In package ISLR, there is a data set called Default.
I want to use that data set, but the ISLR package is not installed on my machine.
data(Default)
# Warning message:
# In data(Default) : data set ‘Default’ not found
library(ISLR)
# Error in library(ISLR) : there is no package called ‘ISLR’
Since I'll probably never use it again, I don't want to install the package. I thought about reading it from the web, but it's not in the linked web page from the package description.
In general, is there a way to load a data set from a package without installing the package?
You can do this from within R:
download.file("http://cran.r-project.org/src/contrib/ISLR_1.0.tar.gz",
dest="ISLR.tar.gz")
untar("ISLR.tar.gz",files="ISLR/data/Default.rda")
L <- load("ISLR/data/Default.rda")
summary(Default)
If you want to keep a copy of the data file:
file.copy("ISLR/data/Default.rda",".")
Clean up:
unlink(c("ISLR.tar.gz","ISLR"),recursive=TRUE)
I'm not sure you can get around having to download the tarball -- in principle you might be able to run untar() directly on a network connection, but I don't think the underlying machinery can actually extract a file without downloading the whole tarball to somewhere on your machine first.
You said, "Since I'll probably never use it again, I don't want to install the package." If the fact that you'll never use it again is your main concern, then perhaps this solution is not quite what you want, but it is probably the simplest solution:
Install the package with install.packages().
Extract and save the dataset that you want.
Uninstall the package with remove.packages().
So the final result is what you want in three simple steps, though the process does involve installing the package, which you hoped to avoid. But you end up without the package in your system that you don't want, so the end result is the same as what you want.

Read Stata 13 file in R

Is there a way to read a Stata version 13 dataset file in R?
I have tried to do the following:
> library(foreign)
> data = read.dta("TEAdataSTATA.dta")
However, I got an error:
Error in read.dta("TEAdataSTATA.dta") :
not a Stata version 5-12 .dta file
Could someone point out if there is a way to fix this?
There is a new package to import Stata 13 files into a data.frame in R.
Install the package and read a Stata 13 dataset with read.dta13():
install.packages("readstata13")
library(readstata13)
dat <- read.dta13("TEAdataSTATA.dta")
Update: readstata13 imports in version 0.8 also files from Stata 6 to 14
More about the package: https://github.com/sjewo/readstata13
There's a new package called Haven, by Hadley Wickham, which can load Stata 13 dta files (as well as SAS and SPSS files)
library(haven) # haven package now available on cran
df <- read_dta('c:/somefile.dta')
See: https://github.com/hadley/haven
If you have Stata 13, then you can load it there and save it as a Stata 12 format using the command saveold (see help saveold). Afterwards, take it to R.
If you have, Stata 10 - 12, you can use the user-written command use13, (by Sergiy Radyakin) to load it and save it there; then to R. You can install use13 running ssc install use13.
Details can be found at http://radyakin.org/transfer/use13/use13.htm
Other alternatives, still with Stata, involve exporting the Stata format to something else that R will read, e.g. text-based files. See help export within Stata.
Update
Starting Stata 14, saveold has a version() option, allowing one to save in Stata .dta formats as old as Stata 11.
In the meanwhile savespss command became a member of the SSC archive and can be installed to Stata with: findit savespss
The homepage http://www.radyakin.org/transfer/savespss/savespss.htm continues to work, but the program should be installed from the SSC now, not from the beta location.
I am not familiar with the current state of R programs regarding their ability
to read other file formats, but if someone doesn't have Stata installed on their computer and R cannot read a specific version of Stata's dta files, Pandas in Python can now do the vast majority of such conversions.
Basically, the data from the dta file are first loaded using the pandas.read_stata function. As of version 0.23.0, the supported encoding and formats can be found in a related answer of mine.
Then one can either save the data as a csv file and import them
using standard R functions, or instead use the pandas.DataFrame.to_feather function, which exports the data using a serialization format built on Apache Arrow. The latter has extensive support in R as it was conceived to promote interoperability with Pandas.
I had the same problem. Tried read.dta13, read.dta but nothing worked. Then tried the easiest and least expected: MS Excel! It opened marvelously. I saved it as a .csv and used in R!!! Hope this helps!!!!

How can i specify R code in the data files in R package

In my R package, I want R to load some data from an external server to main memory. Looking around i found the option of plain R files in the data file section of a package - http://cran.r-project.org/doc/manuals/R-exts.html#Data-in-packages.
But then i couldnt find literature on how the R code will work in the data file section of a package nor how to invoke it.
It would be useful if someone can point me to a sample package too.

Using the "foreign" package in R

I need to import a STATA data set into R and I have downloaded the "foreign" package. Could you please tell me the steps to "load" the package into R and the steps to import the STATA dataset?
R helplist style answer: RTFM!
Statalist style answer: save your Stata file as usual. In R, type
help(package="foreign")
to find out what the commands are. The ones pertaining to Stata would have .dta in them, as .dta is Stata data file extension. read.dta(file="path/name.dta") should work on most occasions. If it does not, try saving your file from Stata as an old version (saveold filename.dta, replace).
BTW, it is Stata, not STATA. It's not an acronym, unlike SAS or SPSS... so you don't have to YELL.
P.S. As DWin correctly pointed out, you need to load the package:
library(foreign)
I assumed that since you seem to know R, remembering that won't be an issue.
It rather depends what you mean by "downloaded". You should not need to download anything, since 'foreign' is included in the standard R installation along with 'base', 'stats', 'utils', 'Matrix', and a few others like 'grDevices'. Whether or not you have already installed the 'foreign' package (unnecessarily) using one of the GUI commands, all you should need to do is:
library(foreign)
?read.dta # and run the example
I just had to deal with the same issue therefore the code:
library(foreign)
setwd(your working directory)
Please note that you have to set the working directory so that R knows where to look for your Stata dta dataset
And last the code:
read.dta("name of the dataset .dta")
A video for that topic:
https://www.youtube.com/watch?v=tCkCz4cu918

Resources