## Settings for RMarkdown http://yihui.name/knitr/options#chunk_options
opts_chunk$set(comment = "", warning = FALSE, message = FALSE, tidy = FALSE,
echo = TRUE, fig.width = 5, fig.height = 5)
options(width = 116, scipen = 10)
setwd("~/Documents/HSPH/useR.at.HSPH/")
Configure RStudio following instructions in: http://www.slideshare.net/kaz_yos/install-and-configure-r-and-rstudio
Create a dedicated R study group folder, set it as the default working directory in RStudio, and put everything related in it. This will save you confusion if you are not sure what a working directory is.
You can also use RStudio menus: Workspaces - Import Dataset - From Text Files…
## Read CSV file (header assumed), then put that into "csv.data" data object (any name is ok).
csv.data <- read.csv("file.csv")
## This gives you a dialogue to choose a file, then the file is passed to read.csv() function
csv.data <- read.csv(file.choose())
## Space-separated (no header assumed)
ssf.data <- read.table("file.dat") # No header
ssf.data <- read.table("file.dat", header = TRUE) # With header
## tab-separated (header assumed)
tsv.data <- read.delim("file.tsv")
install.packages("gdata", dep = T) # If you have not installed it before
library(gdata)
excel.data <- read.xls("file.xls")
install.packages("xlsx", dep = T) # If you have not installed it before
library(xlsx)
## You need to specifiy the sheetIndex (sheet number)
excel.data <- read.xlsx("file.xlsx", sheetIndex = 1)
## Native files
install.packages("sas7bdat", dep = T) # If you have not installed it before
library(sas7bdat)
sas7bdat.data <- read.sas7bdat("file.sas7bdat")
## xport files
install.packages("foreign", dep = T) # If you have not installed it before
library(foreign)
xport.data <- read.xport("file.xpt")
install.packages("foreign", dep = T) # If you have not installed it before
library(foreign) # If you have not loaded the package in the current session.
dta.data <- read.dta("file.xpt")
## Single line per observation
fwf.data <- read.fwf(“file.txt”, width = c(3, 5, ...))
## Multiple lines per observation
fwf.data <- read.fwf(“file.txt”, width = list(c(3, 5, ...), c(5, 7, ...)))
install.packages(“XML”, dep = T)
library(XML)
html.table.data <- readHTMLTable("http://www.drugs.com/top200_2003.html", which = 2, skip.rows = 1)
For detailed example of html table extraction: http://rpubs.com/kaz_yos/1273