vector creation

Double variables

double_var <- c(2,-3.4,0,7.8)

Integer variables

integer_var <- c(2L, 4L, 15L)

Boolean varaibles

logical_var <- c(T, F, F, T)

Character variables

char_var <- c("4", "test", "s")

#Types

typeof(double_var)
## [1] "double"
is.double(double_var)
## [1] TRUE
is.double(integer)
## [1] FALSE

The main way the is function is used:

is.numeric(integer_var)
## [1] TRUE

Type coercion

typeof(c(1L,2L,3))
## [1] "double"

In this example, R is taking the more flexible approach. There are instances where instead you would force it.

as.integer(double_var)
## [1]  2 -3  0  7

As you can see, it is not rounding up the original 7.8, but just taking the integer value. Another use of the as function is taking a vector (either numeric or integer) and making it logical (Boolean). In which positive or negative numbers are TRUE, and 0 is FALSE

as.logical(double_var)
## [1]  TRUE  TRUE FALSE  TRUE

When you force a vector to a numeric type with a mix of potential numeric and non-numeric values, you generate a vector that will store numerics as numerics, and non-numerics as NA

as.numeric(char_var)
## Warning: NAs introduced by coercion
## [1]  4 NA NA