Glossary: Functions

The following is an ever growing list of functions discussed in class. A brief description of the function’s usage is given. The functions’ input (arguments) and output (return value) are also briefly summarized. For full details, check a particular function’s documentation. The descriptions here are not mean to be technical, but instead to remind you about expected usage of these functions. Examples are provided.

A

attributes()

The attributes() function is used access to assign attributes of an object.

  • Input: An R object.
  • Output: A list of attributes, for example names or dimensions.

Examples

attributes(matrix(1:4, ncol = 2))
## $dim
## [1] 2 2
attributes(data.frame(x = 1, y = 3))
## $names
## [1] "x" "y"
## 
## $class
## [1] "data.frame"
## 
## $row.names
## [1] 1
a = 1:10
attributes(a)
## NULL
attributes(a) = list(dim = c(5, 2))
attributes(a)
## $dim
## [1] 5 2

C

c()

The c() function is used to combine objects, most commonly to create atomic vectors, but can be used with other objects. When creating atomic vectors, all elements must be the same type, else R will coerce the elements to be the same type.

  • Input: Objects.
  • Output: An atomic vector or list.

Examples

c(1, 3, 5)
## [1] 1 3 5
c(list(x = 1), list(y = "Hey!"))
## $x
## [1] 1
## 
## $y
## [1] "Hey!"

D

data.frame()

The data.frame() function is used to create data frames.

  • Input: Often a number of atomic vectors with the same length. Could also use other objects with the correct number of rows, such as other data frames.
  • Output: A data frame.

Examples

data.frame(
  x = 1:5,
  y = rep("Hey!", times = 5),
  z = c(TRUE, TRUE, FALSE, TRUE, FALSE)
)
##   x    y     z
## 1 1 Hey!  TRUE
## 2 2 Hey!  TRUE
## 3 3 Hey! FALSE
## 4 4 Hey!  TRUE
## 5 5 Hey! FALSE
data.frame(
  data.frame(x = 1:3),
  data.frame(y = 4:6, z = 5:7)
)
##   x y z
## 1 1 4 5
## 2 2 5 6
## 3 3 6 7

L

length()

The length() function is used to obtain the length of vectors, atomic or lists.

  • Input: An R object, often an atomic vector or list.
  • Output: An integer vector of length 1.

Examples

x = 1:20
length(x)
## [1] 20
y = list(
  x = 1,
  y = "Hello!"
)
length(y)
## [1] 2
z = data.frame(
  x = 1:5,
  y = rep("Hey!", times = 5),
  z = c(TRUE, TRUE, FALSE, TRUE, FALSE)
)
length(z)
## [1] 3

list()

The list() function is used to create lists.

  • Input: Objects.
  • Output: A list.

Examples

list(
  x = 1:10,
  y = "Hello!",
  z = list(
    a = data.frame(
      i = 1:2,
      j = 2:3
    ),
    b = matrix(1:10, nrow = 2)
  )
)
## $x
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $y
## [1] "Hello!"
## 
## $z
## $z$a
##   i j
## 1 1 2
## 2 2 3
## 
## $z$b
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10

N

names()

The names() function is used to obtain or specify the names of an object.

  • Input: An R object.
  • Output: The names of the object as a character vector with the same length as the object. NULL if the object does not have names.

Examples

x = c("uno" = 1, "dos" = 2, "tres" = 3)
names(x)
## [1] "uno"  "dos"  "tres"
y = list(
  x = 1,
  y = "Hello!"
)
names(y)
## [1] "x" "y"
z = 1:10
names(z)
## NULL
names(z) = letters[1:10]
names(z)
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

R

rep()`

The rep()` function is used to obtain or specify the names of an object.

  • Input: An R object, usually a vector.
  • Output: An object of the same type as the input, but usually longer.

Examples

x = c(1, 2, 3)
rep(x, times = 3)
## [1] 1 2 3 1 2 3 1 2 3
rep(x, length.out = 12)
##  [1] 1 2 3 1 2 3 1 2 3 1 2 3
rep(x, each = 3)
## [1] 1 1 1 2 2 2 3 3 3
y = list(
  x = 1,
  y = "Hello!"
)
yy = rep(y, times = 2)
yy
## $x
## [1] 1
## 
## $y
## [1] "Hello!"
## 
## $x
## [1] 1
## 
## $y
## [1] "Hello!"
## [1] 2
length(yy)
## [1] 4
z = 1:4
rep(z, times = c(1, 5, 0, 10))
##  [1] 1 2 2 2 2 2 4 4 4 4 4 4 4 4 4 4