Learning R in Practice

Array

The vector and list are two fundamental array-like types in R. The vector stores the same elements whereas the list can store different type elements. In the following, I will provide different ways to manipulate them.

Vector

Creation of Vector

  • Create number sequence

    • c stands for combination
    • : initiate a sequence with increment of 1 or -1
    • seq(from, to, by=1, length.out=NULL) create a sequence with more flexibility: use by to change the amount of increment; or use length.out to chagne the length of a vector between from and to (inclusive).

      var <- c(1,2,3)             # manually input by 'combine'
      var <- 1:3                  # create c(1,2,3)
      var <- 3:1                  # create c(3,2,1)
      var <- seq(1,3)             # seq() function
      var <- seq(1,3,by=0.5)      # create c(1,1.5,2,2.5,3)
      var <- seq(1,3,length=5)    # create c(1,1.5,2,2.5,3)
      
  • Create repeated numbers

    • c could be used to combine the same variable multiple times to create a vector with repeated numbers.
    • rep(var, times=1, each=1) stands for repeat. The var is a variable to be repeated. The times specifies how many times var will be repeated in sequence. Whereas each first creates the first element in var by the given number times, and then moves to the next one.

      x <- c(1,2,3)       
      var <- c(x,x,x)         # create c(1,2,3,1,2,3,1,2,3)
      var <- rep(x,3)         # same as above and rep(x, times=3)
      var <- rep(x,each=3)    # create c(1,1,1,2,2,2,3,3,3)
      
  • vector with named elements

      var <- c(a=1,b=2,c=3)
    

    And the names of a vector can be retrieved or updated by names()

      names(var)                      # return "a","b","c"
      names(var) <- c("x","y","z")    # rename to "x","y","z"
    

Access of Vector

To access the elements of a vector, we have four approaches: logical, positive integer, negative integer, character;

Positive integer

Different from the commonly used "zero-based indexing", R uses "one-based indexing" to represent the first element in a vector. We can access the elements by their indices within in [].

var[1]      # first element
var[1:3]    # elements from 1 to 3
var[c(1,4)]    # elements at 1 and 4

If the index is 0, R return numeric(0), a vector with lenght 0. If the index is out of the length of a vector, NA will be returned.

How to get index which() can return the index of TRUE values in a vector.

Negative integer

Different from Python, the negative index will return a new vector without the element at the corresponding positive index position.

Logical

Elements at logical TRUE and NA positions specified by an logical vector will be returned.

character

For named vector, we could use a character vector to indicate the elements we want to extract.

var <- c(a=1,b=2,c=3)
var$a                   # return 1

The characters are the names of the elements. We could access and modify them by names() function.

Operation on Vector

One key property in the vector operation is looping. For two vectors with different length, R will automatically loop the shorter one until finish the operation with the longer vector. The most common operation of one vector against a number will apply the same number to each element in that vector (a number is a vector with length=1).

  • Logical operation
    • logic and (intersection) &
    • logic or (union) |
    • >,<,==, etc.
    • identical() will tell whether two vectors are exactly same. This works on NA and NaN. TRUE or FALSE will be returned.
  • Arithmetic operation
  • Character operation paste() will join the character vectors together as appending one afterwards for each element in vectors. Two additional arguments: sep indicates the seperator character among elements; collapse indicates whether we want to combine the output vector into a single character string by specifying a value other than NULL.

List

Vector requires all elements in it are the same type. List looses this requirement.

One thing worths to notice is that the access [] will still return a list. If we want the exact element in that position, we need to use [[]]. Also we could assign the value using `[[]]`` too.