r data types
r data types

R Data Types

Posted on

R Data Types

Variables can store various types of data, each with its own set of functionalities.
In R programming, there’s no need to explicitly declare variable types, and they can even change between types once assigned:

my_var <- 20 # my_var is type of numeric
my_var <- "Ok" # my_var is now of type character (string)

Basic Data Types

Basic data types in R can be divided into the following types:

  • numeric – (1.5, 51, 389)
  • integer – (1L, 50L, 100L, the letter “L” declares this as an integer)
  • complex – (2 + 7i, the letter “i” is the imaginary part)
  • character (string) – (“m”, “my name is Jim”, “FALSE”, “10.5”)
  • logical (boolean) – (TRUE or FALSE)

Examples for baic data types

# numeric
x <- 15.1
class(x)

# integer
x <- 100L
class(x)

# complex
x <- 2 + 7i
class(x)

# character/string
x <- "my name is Jim"
class(x)

# logical/boolean
x <- FALSE
class(x)

> # numeric
> x <- 15.1
> class(x)
[1] “numeric”
>
> # integer
> x <- 100L
> class(x)
[1] “integer”
>
> # complex
> x <- 2 + 7i
> class(x)
[1] “complex”
>
> # character/string
> x <- “my name is Jim”
> class(x)
[1] “character”
>
> # logical/boolean
> x <- FALSE
> class(x)
[1] “logical”

x <- "my name is Jim"