background preloader

Usefull code

Facebook Twitter

Starter code for k fold cross validation using the iris dataset. Passing arguments to an R script from command lines. This post describes how to pass external arguments to R when calling a Rscript with a command line.

Passing arguments to an R script from command lines

The case study presented here is very simple: a Rscript is called which needs, as an input, a file name (a text file containing data which are loaded into R to be processed) and which can also accept an optional additional argument (an output file name: if this argument is not provided, the program supplies one by default). Note: The program just loads a text file containing data, filters out non numeric variables and writes a text file with remaining numeric variables only. R style The most natural way to pass arguments from the command line is to use the function commandArgs. This function scans the arguments which have been supplied when the current R session was invoked. And running the following command line will create a string vector args which contains the entries iris.txt and out.txt.

The simple use case described in the introduction thus gives Finally, the command lines or will give.

Data Imputation

A million ways to connect R and Excel « The R Trader. In quantitative finance both R and Excel are the basis tools for any type of analysis.

A million ways to connect R and Excel « The R Trader

Whenever one has to use Excel in conjunction with R, there are many ways to approach the problem and many solutions. It depends on what you really want to do and the size of the dataset you’re dealing with. I list some possible connections in the table below. 1 – Read Excel spreadsheet in R gdata: it requires you to install additional Perl libraries on Windows platforms but it’s very powerful. require(gdata) myDf <- read.xls ("myfile.xlsx"), sheet = 1, header = TRUE) RODBC: This is reported for completeness only.

Require(XLConnect) wb <- loadWorkbook("myfile.xlsx") myDf <- readWorksheet(wb, sheet = "Sheet1", header = TRUE) xlsx: Prefer the read.xlsx2() over read.xlsx(), it’s significantly faster for large dataset. Date Formats in R. Importing Dates Dates can be imported from character, numeric, POSIXlt, and POSIXct formats using the as.Date function from the base package.

Date Formats in R

If your data were exported from Excel, they will possibly be in numeric format. Otherwise, they will most likely be stored in character format. Importing Dates from Character Format If your dates are stored as characters, you simply need to provide as.Date with your vector of dates and the format they are currently stored in. For example, "05/27/84" is in the format %m/%d/%y, while "May 27 1984" is in the format %B %d %Y. To import those dates, you would simply provide your dates and their format (if not specified, it tries %Y-%m-%d and then %Y/%m/%d): dates <- c("05/27/84", "07/07/05") betterDates <- as.Date(dates, format = "%m/%d/%y") > betterDates [1] "1984-05-27" "2005-07-07"

Download Files from Dropbox Programmatically with R. Tweaking Movie Subtitles with R.

Copulas

Access Google Spreadsheet directly in bash and in R. Google Doc is a good way to share/manage documents between you and your colleagues, but sometime you want to directly access the data in terminal (e.g. bash) or in program (e.g.

Access Google Spreadsheet directly in bash and in R

R), without downloading the data first. For example, I have a Google Spreadsheet here: I want to open it in R or select some of columns in bash. Here are the tips for that: Step1: publish the tab you want to access to the web [howto], in format of CVS or TXT (which is a tab-delimited file actually) Step2: copy the published URL. Step3: To access the file externally. For example, to access the 1st and 3rd columns in bash: wget --no-check-certificate -q -O - ' | cut -f1,3.