Atomic vectors are the most basic data types in R. These are the building blocks of matrices, dataframes, and all of the other commonly used data structures. These are the four most important types of atomic vectors.
Data Types
Double - a number with a decimal
double <- c(1, 0.1, 1.0, 1.01)
is.double(double)
## [1] TRUE
Integer - a whole number
integer <- c(1L, 500L, 0L)
is.integer(integer)
## [1] TRUE
Logical - true or false
logical <- c(T, TRUE, F, FALSE)
is.logical(logical)
## [1] TRUE
Character - a string, or word
character <- c('string_1', "string 2", '345')
is.character(character)
## [1] TRUE
Coercion Rules
Coercion is forcing data from one type to another to make your calculation compatable. You can’t add 1 + ‘seven’, but you can add 1 + 7. Coercion rules are based on which data type retains the most information.
logical to numeric (double or integer)
mixed <- c(T, 1L)
typeof(mixed)
## [1] "integer"
integer to double
mixed <- c(1, 2L)
typeof(mixed)
## [1] "double"
everything to character
mixed_1 <- c('a', 1)
mixed_2 <- c(T, 'a')
mixed_3 <- c('a', 1L)
typeof(mixed_1)
## [1] "character"
typeof(mixed_2)
## [1] "character"
typeof(mixed_3)
## [1] "character"
Forcing Coersion
Some functions will force coersion on their own, this can be useful for counting.
example_log <- c(T, T, F)
example_num <- as.numeric(example_log)
example_num
## [1] 1 1 0
sum(example_log)
## [1] 2
mean(example_log)
## [1] 0.6666667
Other Useful Functions
Here are a few extra useful functions that may be useful when dealing with atomic vector structure.
class()
length()
typeof()
attributes()
That’s all for now!
- Fisher
Comments