Information about the first R Users Group Meeting


Reading and Writing Data in R - 26.04.2015 (Tuesday)

Let’s get it on! Next Tuesday we are going to have our first R Users Group meeting. Josh Madin presents “Reading and Writing Data in R” (script will be provided…Josh?) and afterwards we will have 1 1/2 hours to hack stuff in R. Either we are going to extend the topic presented by Josh, or we are just going to try to solve some glaring questions. So bring some questions, ideas and a charged notebook with R installed.

Absolute beginners can show up at 14:30pm to get some hints about how to get started with R. All others should be at the Biology Tearoom at 15:00pm.

If there are any questions…this is the right time to ask them…



Hi everyone. We’ll develop the script during my presentation, and I’ll post it on this forum afterwards.

After the initial 30 minute spiel, we will have a competition. Whoever can find a (legitimate) dataset that we can’t work out how to read in R wins a chocolate frog. So bring along the weirdest datasets you can find, and we’ll work out together how to load them in R. If the dataset is too large for email, bring it along on a USB stick.


Reading and writing data with R

path <- ""

Reading data tables


data <- read.table("data/seed_root_herbivores.txt")
data <- read.table("data/seed_root_herbivores.txt", sep="\t", header=TRUE)

data <- read.table("data/seed_root_herbivores_prob.txt", sep="\t", header=TRUE)
count.fields("data/seed_root_herbivores_prob.txt", sep="\t")

data <- read.delim("data/seed_root_herbivores.txt")
data <- read.csv("data/seed_root_herbivores.csv")
data <- read.csv("data/seed_root_herbivores_prob.csv")
data <- read.csv("data/seed_root_herbivores_prob.csv", fill = FALSE)

data <- read.csv(paste0(path, "seed_root_herbivores.csv"))

Writing data tables

write.csv(data, "output/my_new_file.csv", row.names=FALSE)
write.table(data, "output/my_new_file.txt", sep="\t", row.names=FALSE)

Saving R objects

x <- 10
save(data, x, file="output/data.RData")

Reading Excel spreadsheets


data <- readWorksheetFromFile("data/Workbook1.xlsx", sheet=1)

data <- readWorksheetFromFile("data/Workbook1.xlsx", sheet=2)

Reading phylogenies


moltree <-"data/Huang&Roy_Molecular.tre")

plot(moltree, "fan", cex=0.4)

Reading Shapefiles


lizard <- readShapeSpatial("data/shapefiles/zone.shp")
reef <- readShapeSpatial("data/shapefiles/dry.shp")

plot(lizard, col="black", asp=1)
plot(reef, col="orange", border="orange", add=TRUE)

Reading NetCDF files


data <- open.ncdf("data/netcdf/")
image(get.var.ncdf(data, "omega")[,,1])
contour(get.var.ncdf(data, "omega")[,,1], add=TRUE)

Reading XML


url <- ""
url <- ""

data <- readHTMLTable(url, stringsAsFactors=FALSE)
data <- data$`NULL`

plot(V4 ~ V1, data, type="l", col="red", ylim=c(0, 32))
lines(V3 ~ V1, data, col="blue")

Reading image files (e.g., jpeg, png)


img <- readJPEG("data/images/AC0048_F4_T_A_C.JPG")

plot(NA, xlim=c(1,dim(img)[2]), ylim=c(1,dim(img)[1]), asp=1)
rasterImage(img, 1, 1, dim(img)[2], dim(img)[1])

lm <- read.table("data/images/AC0048_F4_T_A_C.lm")
points(lm[,1], dim(img)[1] - lm[,2], col = "blue", pch = 20)

ol <- read.table("data/images/AC0048_F4_T_A_C.ol")
lines(ol[,1], dim(img)[1] - ol[,2], col = "red", lwd = 2)


You won’t have this datafile.

data <- readMat("data/total_mesh_r20150519_011620_lizard_d2_011_horse_shoe_circle01.mat")$data[,,1]$xyz

plot3d(data, aspect=c(1, 1, 0.1), col = grey((data[,3]-min(data[,3]))/(diff(range(data[,3])))))


I thought that these little tricks provided by Daniel Falster might be helpful for some people as they have been for me…Here is also a LINK to his website where he supplies us with some cool R tips and further links…

  1. When reading csv files with read.csv most people now set the option stringsAsFactors=FALSE. Factors are a bit of curse and mostly worth avoiding.sometimes you might want to set check.names=FALSE to stop R changing the column header

  2. Wrapper functions: If you are reading lots of files in, it can be helpful to write a wrapper function to save you repeating all your custom option
    E.g. instead of writing
    f1 <- read.csv(file1, stringsAsFactors=FALSE, check.names=FALSE)
    f2 <- read.csv(file2, stringsAsFactors=FALSE, check.names=FALSE)
    read_file <- function(…) {
    ** read.csv(…., stringsAsFactors=FALSE, check.names=FALSE)**

f1 <- read_file(file1)
f2 <- read_file(file2)

  1. For large files, the package readr has a replacement for
    read.csv which is about 10 x faster and sets stringsAsFactors as FALSE
    by default

f1 <- read_cv(file1)

  1. You can the function readHTMLTable within the package XML to extract tables from HTML files.


Thanks to both of You, Josh and René.