R Commands

The R language has numerous functions. Here is a small collection of very useful R commands.

General

help

# Open help in browser
options(help_type='html') 

# Open help internally
options(help_type='text')

options

# Do not print numeric values in exponential notation
options(scipen=100)

function

# Function with 'super assignment' operator '<<-'
a=3
b=3
mf=function() {
  a<<-a+9
  b=b+9
  return(paste('a=',a,'b=',b))
}
out=mf()
out
#[1] 'a= 12 b= 12'

paste('a=',a,'b=',b)
#[1] 'a= 12 b= 3'

http://article.gmane.org/gmane.comp.lang.r.general/227066

Input

download.file

# Download file from the United Nations website
url.c='http://unstats.un.org/unsd/demographic/products/socind/Dec.%202012/1a.xls'
dfile='1a.xls'
download.file(url.c, destfile=dfile, method='wget')

dir.create

# Create directory Data
if(file.exists('Data/')==F){dir.create('Data/')}

list.files

# List all .csv files in current directory
list.files('.', pattern='*.csv', full.names=FALSE)

read.csv

# Read all columns as character
dat=read.csv(file,head=T,sep=';',dec='.',colClasses = 'character')
# Read file with umlauts
dat=read.csv(file, head=T, sep=';', dec=',', encoding='latin1')

system

Invoke a system (bash) command

# List files of current directory
system('ls -l')

R objects

save

# Save all R objects
save(list = ls(), file='all.RData')

load

# Restore R objects
load('all.RData', .GlobalEnv)

rm

# Remove all objects
rm(list=ls())
# Remove all objects but not selected list
selc=c('dat1','dat2','dat3')
rm(list=(subset(ls(),is.element(ls(),selc)==F)))

Data preparation

which

# Give the indices of value 6
x=c(1,2,6,8,6,0)
which(x==6)
#[1] 3 5

replace

One value:

# Replace position 1 and 2 in dat with 9
dat=c(3,6,2,7,1)
replace(dat,c(1,2),9)
#[1] 9 9 2 7 1

# Replace 7 with 9
replace(dat,which(dat==7),9)
#[1] 3 6 2 9 1
or:
# Replace 7 with 9
dat=c(3,6,2,7,1)
dat[dat == 7] = 9
dat
#[1] 3 6 2 9 1

Multiple values:

# Replace multiple values in vector at once
d=cbind(c(1:5),c(1,5,2,7,14),c(11:15))
x=c(1,2,7,14)
y=c('eins','zwei','sieben','vierzehn')
sapply(1:length(x), function(i) {d[d==x[i]]<<-y[i]})

gsub

# Replace 7 with 9 in dat
dat=c(3,6,2,7,1)
gsub(7, 9, dat)
# Point as thousand seperator
a='3.466,33'
a=gsub('.','',a)
a=gsub(',','.',a)
as.numeric(a)
#[1] 3466.33

paste

# sep
x=c(1:4)
y=c(5:8)
paste(x,y,sep=',')
# collapse
paste(x,collapse=',')

sprintf

# n-stelliger Integer
sprintf('%03d', 7)
#[1] "007"

nchar

# Number of letters in a character
ntxt='Example'
nchar(ntxt)
#[1] 7

substring

# Letter one to four of a character
ntxt='Example'
substring(ntxt,1,4)

quotes

http://stat.ethz.ch/R-manual/R-devel/library/base/html/Quotes.html

# Example: apostrophe
cat('following in 'apostrophe'')
following in 'apostrophe'

Data Analysis

apply, sapply, lapply

http://climvis.de/how-to-use-apply-sapply-lapply-in-r/

subset

# Subset
k=c(-10:10)
subset(k, k>=-5 & k<=5)
#[1] -5 -4 -3 -2 -1  0  1  2  3  4  5</pre>
<h2>is.element</h2>
<pre>
# Is x element of y
x=c(1,2,6,8,6,0)
y=c(3,6,9,1)
is.element(x,y)
#[1]  TRUE FALSE  TRUE FALSE  TRUE FALSE

intersect

## Intersection of x and y
x=c(1,2,6,8,6,0)
y=c(3,6,9,1)
intersect(x,y)
#[1] 1 6

duplicated

# which elements of x are duplicates
x=c(1,2,6,8,6,0)
duplicated(x)
#[1] FALSE FALSE FALSE FALSE  TRUE FALSE

match

# position of x in y
# ‘match’ returns a vector of the positions of (first) matches
# of its first argument in its second.
mx=matrix(c(1:10),nrow=5,ncol=2)
my=matrix(c(3:8),nrow=3,ncol=2)
match(mx[,1],my[,1])
#[1] NA NA 1 2 3

# my in same order as mx[,1]
my[match(mx[,1],my[,1]),]
#   [,1] [,2]
#[1,] NA NA
#[2,] NA NA
#[3,] 3 6
#[4,] 4 7
#[5,] 5 8

order

# Order matix m by second column
m=matrix(c('A', 'B', 'C', 5, 1, 7), nrow=3, ncol=2)
m[order(m[,2], decreasing=F), ]

Date-Time

as.POSIXct

# Conversion from Character to Date-time
as.POSIXct('2013-01-01 10:15', format='%Y-%m-%d %H:%M', tz='UTC')

# format info needed when time is in standard format
as.POSIXct('2013-01-01 10:15', tz='UTC')

# JavaScript date based on a time value that is milliseconds
# since 1970-01-01 00:00 UTC
as.POSIXct(1138683600000/1000, origin='1970-01-01 00:00', tz='UTC')
# "2006-01-31 05:00:00 UTC"

strptime

# Conversion from Character to Date-time
strptime('2013-01-01 10:15', format='%Y-%m-%d %H:%M', tz='UTC')

Plot

png

# png with width, height and pointsize
png('file.png',width=1000, height=500, pointsize = 12)
dev.off()

par

Set or Query Graphical Parameters

# Graphic with two rows and one column and a text size 1.6
# relative to the default
par(mfrow=c(2,1), cex=1.6)

colors

http://www.colorpicker.com/
http://colorbrewer2.org/
http://www.stat.ubc.ca/~jenny/STAT545A/block14_colors.html#rcolorbrewer
http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf

barplot(rep(1,5),yaxt='n', col=c('darkblue', 'blue', 'lightblue', 'lightpink', 'brown3'))

colors

Color Palettes

# 10 rainbow colors
rainbow(10)

colorRampPalette

# Color interpolation
barplot(rep(1,5),yaxt='n',col=colorRampPalette(c('blue','green'))(5))
title("colorRampPalette(c('blue','green'))(5)")

colorRampPalette