9 Base R

9.1 Data Types

9.1.1 Numeric (Double)

  • Can include integers and decimal numbers (continuous variables)
  • EXAMPLE:
is.numeric(c(1, 2, 3.5, 6.7))
## [1] TRUE
is.numeric(c(1, 2, 3, 6))
## [1] TRUE
is.numeric(1:5)
## [1] TRUE
is.numeric(c(TRUE, FALSE, TRUE))
## [1] FALSE
is.numeric(c("a", 2, 3, 6))
## [1] FALSE
is.numeric(c("a", "b", "c", "d"))
## [1] FALSE

9.1.2 Integer

  • Only whole numbers (discrete variables)
  • From is.integer()help file: “is.integer(x) does not test if x contains integer numbers!”
  • EXAMPLE:
is.integer(c(1, 2, 3.5, 6.7))
## [1] FALSE
is.integer(c(1, 2, 3, 6))
## [1] FALSE
is.integer(1:5)
## [1] TRUE
is.integer(c(TRUE, FALSE, TRUE))
## [1] FALSE
is.integer(c("a", 2, 3, 6))
## [1] FALSE
is.integer(c("a", "b", "c", "d"))
## [1] FALSE

9.1.3 Logical

  • TRUE or FALSE values
  • EXAMPLE:
is.logical(c(1, 2, 3.5, 6.7))
## [1] FALSE
is.logical(c(1, 2, 3, 6))
## [1] FALSE
is.logical(1:5)
## [1] FALSE
is.logical(c(TRUE, FALSE, TRUE))
## [1] TRUE
is.logical(c("a", 2, 3, 6))
## [1] FALSE
is.logical(c("a", "b", "c", "d"))
## [1] FALSE

9.1.4 Character

  • Alphanumeric
  • EXAMPLE:
is.character(c(1, 2, 3.5, 6.7))
## [1] FALSE
is.character(c(1, 2, 3, 6))
## [1] FALSE
is.character(1:5)
## [1] FALSE
is.character(c(TRUE, FALSE, TRUE))
## [1] FALSE
is.character(c("a", 2, 3, 6))
## [1] TRUE
is.character(c("a", "b", "c", "d"))
## [1] TRUE

9.1.5 Factor

  • A special integer vector to assign order
  • Use: assign custom order to categorical variables
    • If you were to sort a character vector, R would sort the vector alphabetically.
  • EXAMPLE:
is.factor(c(1, 2, 3.5, 6.7))
## [1] FALSE
is.factor(c(1, 2, 3, 6))
## [1] FALSE
is.factor(1:5)
## [1] FALSE
is.factor(c(TRUE, FALSE, TRUE))
## [1] FALSE
is.factor(c("a", 2, 3, 6))
## [1] FALSE
is.factor(c("a", "b", "c", "d"))
## [1] FALSE

Example of sorting a character vector.

sort(c("a", "b", "c", "d"))
## [1] "a" "b" "c" "d"

Example of sorting a factor vector.

sort(factor(c("a", "b", "c", "d"),
            levels = c("d", "a", "c", "b")))
## [1] d a c b
## Levels: d a c b
as.numeric(sort(factor(
  c("a", "b", "c", "d"),
  levels = c("d", "a", "c", "b")
  )))
## [1] 1 2 3 4

9.1.5.1 Example

In this example there are three stations on the Hudson River (i.e., Port of Albany, West Point, and Pier 84).

Although this is a tidal river, we would generally want to sort and plot this data from upstream to downstream (i.e., Port of Albany, West Point, and Pier 84). The table below shows how sort() would arrange the data if it is stored as a character vs. factor type.