Data Types in R Programming Language: Overview and Examples
Table of Content:
In contrast to other programming languages like C and Java in R, the variables are not declared as some data type. The variables are assigned with R-Objects and the data type of the R-object becomes the data type of the variable. There are many types of R-objects. The frequently used ones are ?
- Vectors
- Lists
- Matrices
- Arrays
- Factors
- Data Frames
The simplest of these objects is the vector object and there are six data types of these atomic vectors, also termed as six classes of vectors. The other R-Objects are built upon the atomic vectors.
Data Type | Example | Verify |
---|---|---|
Logical | TRUE, FALSE | var <- TRUE print(class(var)) it produces the following result ? [1] "logical" |
Numeric | 12.3, 5, 999 | var <- 23.5 print(class(var)) it produces the following result ? [1] "numeric" |
Integer | 2L, 34L, 0L | var <- 2L print(class(var)) it produces the following result ? [1] "integer" |
Complex | 3 + 2i | var <- 4+5i print(class(var)) it produces the following result ? [1] "complex" |
Character | 'a' , '"good", "TRUE", '23.4' | var <- "TRUE" print(class(var)) it produces the following result ? [1] "character" |
Raw | "Hello" is stored as 48 65 6c 6c 6f | var <- charToRaw("Hello") print(class(var)) it produces the following result ? [1] "raw" |
Date | "2019-08-07" | date <- as.Date("2019-08-07") class(date) it produces the following result ? [1] "Date" date <- as.POSIXct("2019-08-07") class(date) it produces the following result ? [1] "POSIXct" "POSIXt" |