R Tutorial R Charts & Graphs R Statistics R Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

R - Data Types



One of the most important parts of learning any programming language is to understand what are the available data types, and how data is stored, accessed and manipulated in that language. In R, different types of data can be stored using variables.

In R, there are six basic data types which are categorized below:

  • Integer
  • Numeric
  • Complex
  • Logical
  • Character
  • Raw

In R, when a variable is assigned with a R-Object, the data type of the R-object becomes the data type of the variable.

R Basic Data Types

The table

Data TypesExampleDescription
Integer10L, 55LL tells R to store the value as an integer.
Numeric 10, 10.5It is the default computational data type in R.
Complex1+2i, 5-3i Here, i is used to define imaginary part.
LogicalTRUE, FALSERepresents boolean values. It has only two possible values - true/false.
Character"Hello", "FALSE", "Hi All", "10.5"Represents the string value.
Raw A raw data type is used to holds raw bytes.

Example:

The R class() function can be used to check the data type of a variable:

#Integer Data type  
var_integer <- 55L  
cat("var_integer:", var_integer,"\n")   
cat("Data type of var_integer:", class(var_integer),"\n\n")  

#Numeric Data type  
var_numeric <- 10.5  
cat("var_numeric:", var_numeric,"\n")   
cat("Data type of var_numeric:", class(var_numeric),"\n\n")  

#Complex Data type  
var_complex <- 1+2i
cat("var_complex:", var_complex,"\n")   
cat("Data type of var_complex:", class(var_complex),"\n\n")  

#Logical Data type  
var_logical <- TRUE
cat("var_logical:", var_logical,"\n")   
cat("Data type of var_logical:", class(var_logical),"\n\n")  
   
#Character Data type  
var_character <- "Hello World"
cat("var_character:", var_character,"\n")   
cat("Data type of var_character:", class(var_character),"\n\n")    
  
#Raw Data type  
var_raw <- charToRaw("Hello World")  
cat("var_raw:", var_raw,"\n") 
cat("Data type of var_raw:", class(var_raw),"\n\n") 

The output of the above code will be:

var_integer: 55 
Data type of var_integer: integer 

var_numeric: 10.5 
Data type of var_numeric: numeric 

var_complex: 1+2i 
Data type of var_complex: complex 

var_logical: TRUE 
Data type of var_logical: logical 

var_character: Hello World 
Data type of var_character: character 

var_raw: 48 65 6c 6c 6f 20 57 6f 72 6c 64 
Data type of var_raw: raw 

5