Introduction to R

This is a very simple Introduction to R to allow users to become used to working with data in R.  R is available for Windows, Linux and MacOS from http://www.r-project.org

We also recommend use of a text editor that knows about R code - such as Tinn-R: http://www.sciviews.org/Tinn-R/.  The code below is executable and can be pasted directly into R.  Lines and text that begin with # are comments that will be ignored by R.  Lines that are red are code and will be interpreted.


# variables - storing information
# simple calculations

10
10 * 20
10^4

# simple calculations of a variable
x <- 10
x
x * 20
x^4

# vectors
c(1,2,3,4)

# what does c() mean?
# c is a function - combine

?c

# lets assign the vector 1,2,3,4 to variable v
v <- c(1,2,3,4)
v

# simple calculations on a vector
v * 20
v^4
x <- v ^ 4
v
x

# acessing parts of vectors
x[1]
x[2]
x[c(1,4)]

# useful functions to apply to vectors
sum(v)
mean(v)
length(v)

# generating sequences of numbers
1:10
c(1:10) 
# exactly the same thing

seq(1,10, by=1)
seq(1,10, by=.5)

x <- 1:10
rep(x, times=3)
rep(x, times=3, each=5)

# logical vectors
x <- rep(1:10,times=3)
x > 5
gt5 <- x > 5
gt5
x[gt5]

# matrices
x <- array(1:20, dim=c(4,5))   
x
x*20
x^2

x[1,2] # get element in first row, second column
x[1,]  # get first row
x[,3]  # get third column

t(x)  # transpose the matrix

y <- array(20:40, dim=c(4,5))

cbind(x,y) # combine by columns
rbind(x,y) # combine by rows

# lists - like a named vector
# a list is kind of like a vector, with the elements named as well as numbered:

lst <- list(name="Fred", wife="Mary", no.children=3,
                   child.ages=c(4,7,9))
                   
lst

lst[[1]]   # get the first element in the list
lst$name   # get the value in the list referred to by 'name'

# data frames - useful matrices :)
mirrors <- read.csv(paste(R.home("doc"), "CRAN_mirrors.csv", sep="/"), row.names=1)
# mirrors <- getCRANmirrors(all = FALSE, local.only = FALSE)     # alternative

is.data.frame(mirrors) # check and see if mirrors is a data frame

mirrors[1:5, 1:3]  # mirrors can be accessed in the same way as a matrix

colnames(mirrors)  # column names
rownames(mirrors)  # row names

mirrors$Host  # columns can be accessed like a list
mirrors$URL

# we can also access them using the [..,..] notation and names, instead of numbers
mirrors[1:5,1:3]
mirrors[1:5,c("Country","City","URL")]
mirrors["Austria", 1:3]

# save data
save.image()