Data Types

Usually in other programming languages, you declare variables with some data type to reserve memory space for that variable.

In R, Variables are not declared with data types like in other programming languages. In R, the variables are assigned with R-Objects and hence the data type of the R-object becomes the data type of the variable. Some of the R-objects are as follows:

  • Vectors
  • Lists
  • Arrays
  • Data Frames
  • Matrices
  • Factors

Let's discuss more in-detail about the above R-objects in coming chapters.

There are six data types of the atomic vectors and usually the R-Objects are built upon the atomic vectors. These are also called as six classes of vectors.

Data typeDescriptionUsage
NumericTo represent decimal valuesx=1.84
IntegerTo represent integer values, L tells to store the value as integerx=10L
ComplexTo represent complex valuesx = 10+2i
LogicalTo represent boolean values, true or falsex = TRUE
CharacterTo represent string valuesx <- "One compiler"
rawHolds raw bytes
var_logical <- FALSE  # Logical variable
cat("The data type of", var_logical," :",class(var_logical),"\n")  
  
var_numeric <- 797 # Numeric variable
cat("The data type of", var_numeric," : ",class(var_numeric),"\n")  
  
var_integer <- 53L  # integer variable
cat("The data type of", var_integer," : ",class(var_integer),"\n")  
  
var_complex <- 5+2i  # complex variable
cat("The data type of ", var_complex ,":",class(var_complex),"\n")  
  
var_char<- "One Compiler"  # character variable
cat("The data type of ", var_char ,":",class(var_char),"\n")  
  
var_raw <- charToRaw("Hello World")  # raw variable
cat("The data type of var_raw :",class(var_raw),"\n")
cat("var_raw is stored as", var_raw)

Run here