Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Sample slides from "Programming with R" course

2,350 views

Published on

Sample slides from "Programming with R" course, run in collaboration with PRISM TC http://www.prismtc.co.uk/f107-r-programming-course-2/.

Published in: Data & Analytics
  • Be the first to comment

  • Be the first to like this

Sample slides from "Programming with R" course

  1. 1. © 2012 Heather Turner Part I Writing R Code 5 / 80
  2. 2. © 2012 Heather Turner Data Structures Data structures are the building blocks of code. In R there are four main types of structure: • vectors and factors • matrices and arrays • lists • data frames 6 / 80
  3. 3. © 2012 Heather Turner Vectors Vectors are one dimensional data objects > c(1, 2, 3) [1] 1 2 3 The elements must all be of the same type: logical, numeric or character, and will be coerced if necessary: > c(NA, FALSE, 1) [1] NA 0 1 > c(NA, TRUE, 1, "a") [1] NA "TRUE" "1" "a" The functions as.logical, as.numeric and as.character will coerce to the corresponding type, producing NAs when coercion fails. 7 / 80
  4. 4. © 2012 Heather Turner Logical Vectors Logical vectors are commonly used for indexing > x <- c(1, 1, 2, 2) > x[x > 1] [1] 2 2 The logical binary operators are >, <, ==, <=, >= and !=, whilst ! is a unary operator. Some functions also return logical vectors, e.g. duplicated: > duplicated(x) [1] FALSE TRUE FALSE TRUE > !duplicated(x) [1] TRUE FALSE TRUE FALSE Single element logical vectors (scalars) are useful for flow control, see later. 8 / 80
  5. 5. © 2012 Heather Turner Numeric Vectors The are several convenience function for creating numeric vectors, the most common being seq and rep. As they are so useful there are fast shortcuts for particular cases > x <- 3:5 > seq_len(3) [1] 1 2 3 > seq_along(x) [1] 1 2 3 > rep.int(1:2, times = c(2, 3)) [1] 1 1 2 2 2 9 / 80
  6. 6. © 2012 Heather Turner Character Vectors Character vectors may be used for creating names > names(x) <- paste(LETTERS[1:3], 1229:1231, sep = "") > x A1229 B1230 C1231 3 4 5 Useful functions to extract information from character vectors include nchar, substr, strsplit, grep and gsub. The last two are for pattern matching and replacement > grep("123", names(x), fixed = TRUE) [1] 2 3 > as.numeric(gsub("[A-C]", "", names(x))) [1] 1229 1230 1231 The latter uses a regular expression, see references. 10 / 80
  7. 7. © 2012 Heather Turner Factors Factors are a special type of vector representing a categorical variable as a vector of numeric codes with an attribute giving the levels > colour <- factor(rep(c("red", "green", "blue"), 2)) > str(colour) Factor w/ 3 levels "blue","green",..: 3 2 1 3 2 1 The function gl is useful for creating factors > gl(3, 1, length = 6, labels = c("blue", "green", "red")) [1] blue green red blue green red Levels: blue green red > gl(3, 2, length = 6, labels = c("red", "green", "blue")) [1] red red green green blue blue Levels: red green blue 11 / 80

×