R Examples

ls lsf.str
# List all loaded packages
(.packages())
# List all objects in a package
ls('package:stats')
# List all functions in a package (also give the call sequences)
lsf.str('package:stats')
scan read.fwf
scan('http://www.ats.ucla.edu/stat/R/modules/scan.txt',
	what = list(age = 0, name = ''))

# Read only the names as a vector
x = scan('http://www.ats.ucla.edu/stat/R/modules/scan.txt',
	what = list(NULL, name = ''))
x[sapply(x, length) > 0]

# Read fixed-width files
colnames = scan('http://www.ats.ucla.edu/stat/R/faq/names.txt',
	what = character())
read.fwf('http://www.ats.ucla.edu/stat/R/faq/testfixed.txt',
	col.names = colnames, width = c(5, 7, 2, 4, 4))
cat
cat('
Girth Height Volume
  8.3     70   10.3
  8.6     65   10.3
  8.8     63   10.2
 10.5     72   16.4
 10.7     81   18.8
 10.8     83   19.7
', file = 'tmp.data')
d = read.table('tmp.data', header = TRUE)
unlink('tmp.data')
which
mat = matrix(round(runif(15) * 20), nrow = 3,
	dimnames = list(paste('R', 1:3, sep = ''), paste('C', 1:5, sep = ''))
); mat

# Method 1
lst = data.frame(
	rowname = rep(rownames(mat), ncol(mat)),
	colname = rep(colnames(mat), each = nrow(mat)),
	value = as.vector(mat)
)
lst[lst$value < 10, ]

# Method 2
indices = which(mat < 10, arr.ind = TRUE)
data.frame(
	rowname = rownames(mat)[indices[, 1]],
	colname = colnames(mat)[indices[, 2]],
	value = mat[which(mat < 10)]
)
reshape
d = data.frame(id = c(1,2), red = c(70,80), green = c(50,60), blue = c(30,40)); d

reshape(d, varying = names(d)[-1], v.names = 'value', timevar = 'varname',
	idvar = 'id', times = names(d)[-1], direction = 'long')
function
# Modifying an already existing function by using the technique
# of passing an unspecified number of parameters to a function
redPlot = function(..., color = 'red') plot(..., col = color)

plot(1:10, rnorm(10))
redPlot(1:10, rnorm(10))
paste assign get formula
for (i in 1:3) {
	xname = paste('x', i, sep = '')
	assign(xname, rnorm(100))
	yname = paste('y', i, sep = '')
	assign(yname, 3 + 7 * get(xname) + rnorm(100))
	formula = as.formula(paste(yname, ' ~ ', xname, sep = ''))
	print(formula)
	print(lm(formula))
}
par clip
x = rnorm(1000)
hist(x, xlim = c(-4, 4))
usr = par('usr')
clip(usr[1], -2, usr[3], usr[4])
hist(x, col = 'red', add = TRUE)
clip(2, usr[2], usr[3], usr[4])
hist(x, col = 'blue', add = TRUE)
do.call('clip', as.list(usr))  # reset to plot region
system.time
nrow = 20
ncol = 200000
mat = matrix(round(runif(nrow * ncol) * 50), nrow = 10); dim(mat)
d = data.frame(mat); dim(d)

loop = function(mat) {
	n = ncol(mat)
	res = numeric(n)
	for (i in 1:n) { res[i] = mean(mat[ , i]) }
	return(res)
}

system.time(loop(mat))
system.time(apply(mat, 2, mean))
system.time(sapply(d, mean))

# "apply" uses a loop internally, so it is simpler but sometimes slower than a loop
# Other *apply functions are more efficient because they are written in C
sprintf environment
# "<<-" causes a search to make through the environment for
# an existing definition of the variable being assigned

rm(list = ls())

f1 = function() {
	x = x + 1
	y <<- y + 1
	a = 10
	b <<- 20
	print(sprintf('f1: x = %d, y = %d', x, y))
	print(sprintf('f1: %s', paste(ls(), collapse = ', ')))
		# 'y' and 'b' are not in the current environment
}

f2 = function() {
	environment(f1) = environment()
	x = 1
	y = 2
	f1()
	print(sprintf('f2: x = %d, y = %d', x, y))
		# 'y' is modified since "f1" makes through
	print(sprintf('f2: %s', paste(ls(), collapse = ', ')))
}

f1() # Error in f1() : objects 'x' not found
f2() # Notice different assignment operations in 'f1'
ls() # 'b' is created in GlobalEnv since "f1" keeps making through
     # environments until it finds 'b' or it reaches GlobalEnv
assign
cat
clip
environment
formula
function
get
ls
lsf.str
par
paste
read.fwf
reshape
scan
sprintf
system.time
which