Import data from Excel to R using gdata library


There are several ways to import data into R.

The standard way, what it used to be, is from a text file using read.table() function.

For excel files, the most famous spreadsheet software on the world, several libraries can be used to import data from .xls file, for example

 RODBC 

. In the past, the problem was with the xlsx file, which was not supported yet.

Recently, I discovered that

 gdata 

can be used to import xlsx file now. So, this bypass the step that I normally have to save the excel file to text file and do the regular file import.

Here’s how:

library(gdata)
data <- read.xls(xls="myData.xlsx",sheet=1,header=TRUE, as.is=TRUE)
Advertisements

Transpose Table Sideway


I’ve come across a problem needing to transpose to wide table into a long format. I’m not talking about the longitudinal data quite yet, the one where you have one individual getting multiple measurements over time.

The question then is get a lot simpler than having to manipulate longitudinal data, which you can do with

library(reshape)

in R. See:

 

?melt
?cast

 

 

Recently, the

library(data.table)

has come into my rescue. With fread function reading in large data frame (or data table) has become much faster. Therefore, base on the simple fread and write.table. here comes the transpose function. You can get the script from my short script transposeR.r github Genetics Library (which has just recently been updated).

Rscript transposeR.r data_1.txt data_2.txt

You can also use wildcard.

Rscript transposeR.r data_?.txt

I mostly tested it on mac, if your windows machine doesn’t play with ls command then, the script might not work with multiple file wildcard.

A new way to install Bioconductor


In the past, whenever we want to install a package on Bioconductor. The first thing that we have to do is to “source” the

source("http://bioconductor.org/biocLite.R")

The good news is that there is a new package “BiocInstaller”, which can help you install Bioconductor package.

Here’s an example to install a library that will help to work with NHGRI’s GWAS catalog

install.packages("BiocInstaller")
library(BiocInstaller)
biocLite("gwascat")

Sourcing R script over HTTPS – Stack Overflow


You can use

source("http://yourcode.com/rcode.R")

to source your script from online resource.
However, source doesn’t work if you have https link,

Here’s some solutions I found on stack overflow.

library(RCurl)
eval(expr = parse( text = getURL"http://www.mayin.org/ajayshah/KB/R/tutorial.R", ssl.verifypeer=FALSE))

via Sourcing R script over HTTPS – Stack Overflow.

Configuring R to Use an HTTP Proxy / FAQ / Knowledge Base – RStudio Support


Feel like I’ve done this a million time since I came back to work in Thailand. This should work for R user not using R-studio as well. Configuring R to Use an HTTP Proxy / FAQ / Knowledge Base – RStudio Support. In brief, you can use the command template below in each of the R session.


Sys.setenv(http_proxy="http://user:password@proxy_server:port")

I found that this is more convenient for me who carry my laptop around. And the only place that won’t let me work at ease is when I am at the office. So, the work around will need to be used occasionally.

If you would like to check your current proxy setting, you can do


Sys.getenv("http_proxy")

*Note*: Please note the capital “S” at the beginning of the command follows by all small letters afterwards.

R FAQ: How can I format a string containing a date into R “Date” object??


This sounds like a problem that statisticians occasionally have to deal with. It is quite simple, just convert the string to date or time if you remember what command to use. If your date variable has a value like this 2007-05-31 for May 31, 2007. You can simply use the example below.


as.Date(DATE_VARIABLE,format='%Y-%m-%d')

For more example on date, I refer you to UCLA R-help page:

R FAQ: How can I format a string containing a date into R “Date” object??.

For a complete list of date-time formatting conversion see this


?strptime