Introduction

Within this document, we will practice how to use R markdown to format our document for a reproducible research. Then introduce what we have learned in class 2 about data types, objects, and basic operations.

Data types

Atomic data types

Within R, there are 6 common atomic data types:

  1. Character, e.g. c('geospatial', 'analysis', 'R').
  2. Double, e.g. c(3.14, 20.1, 79.6).
  3. Integer, e.g. c(10L, 40L).
  4. Logical, e.g. c(TRUE, FALSE, FALSE).
  5. Another two uncommon data types:
    • Complex
    • Raw

Other special types

  • NULL, which means not exist.
  • NA, which means missing value. It is a special logical type by default, but could add into any types of vectors.
  • Inf/-Inf, which means infinity. They are special double type.
  • NaN, which means undefined value. Usually it could be used as missing value as well.

Some useful classes

  1. Factor, which is an integer type with class “factor” for categorical data. E.g. factor(c('F', 'M)). Each value has a corresponding label.
  2. Date, which is a double type with class “Date” for date. E.g. 2026-01-25 20:44:34.815758, as.Date('2021-01-01').
  3. Time, which is a double type with class c(“POSIXct” “POSIXt”) for time. E.g. as.POSIXct('1980-01-01').

Objects

Atomic vector

The most basic object in R is atomic vector. It only takes the same data type. If it is fed with different data types, the items will be converted based on the rule: \[logical < integer < double < character\] We could use function c to concatenate multiple items within a vector. When a vector has dimensions, it will become a matrix (2 dimensions), or array (3 or more dimensions). We could use function rbind or cbind to bind multiple vectors into matrix.

An example

x <- c(1:10)
y <- c(5:14)
m <- rbind(x, y)
m
##   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## x    1    2    3    4    5    6    7    8    9    10
## y    5    6    7    8    9   10   11   12   13    14

List

Being very similar to vector, but list can store items of different data types.

Attributes

Objects have attributes. The most common ones are:

  • names, and rownames, colnames and dimnames if has dimensions.
  • length, and dim if has dimensions.
  • class if it belongs to a class.

Function attr() can be used to get or set an attribute of an object. Function attributes() can get all attributes of an object.