An overview of data types
The following is a super detailed table summarizing different data types and objects in R. I found this in tibble
. However, you can just pay most attention to the commonly used ones:)
Class | Data type | Example | Column header |
---|---|---|---|
Atomic | logical | TRUE |
lgl |
integer | 1L |
int | |
double | 1.5 |
dbl | |
character | "A" |
chr | |
complex | 0+1i |
cpl | |
raw | as.raw(1) |
raw | |
list | list(1) |
list | |
named list | list(a = 1) |
named list | |
Built-in objects | factor | factor("A") |
fct |
ordered | ordered("a") |
ord | |
Date | Sys.Date() |
date | |
POSIXt | Sys.time() |
dttm | |
difftime | vctrs::new_duration(1) |
drtn | |
Objects from other packages | hms | hms::hms(1) |
time |
integer64 | bit64::as.integer64(1e+10) |
int64 | |
blob | blob::blob(raw(1)) |
blob | |
Data frames | data.frame | data.frame(a = 1) |
df[,1] |
tbl_df | tibble(a = 1) |
tibble | |
Unchanged | AsIs | I(1L) |
I |
vctrs types | unspecified | vctrs::unspecified(1) |
??? |
vctrs_list_of | vctrs::list_of(c(1L)) |
list |
|
vctrs_vctr | vctrs::new_vctr(1L) |
vctrs_vc | |
vctrs_partial_factor | vctrs::partial_factor(letters) |
prtl_fctr | |
vctrs_partial_frame | vctrs::partial_frame(a = 1) |
prtl | |
Language objects | function | function() NULL |
fn |
symbol | quote(a) |
sym | |
expression | parse(text = "a <- 1\nb<- 2") |
expression | |
quosures | rlang::quos(a = 1) |
quos |
Determining the type of an object: typeof(x)
and mode(x)
To determine the type or storage mode of any R object, you can use typeof(x)
and mode(x)
.
the input x could be any R object. The potential output could be “logical”, “integer”, “double”, “complex”, “character”,“list”, “NULL”, “closure” (function), etc.
Here are some examples:
my.examples | typeof.out | mode.out | class.out | |
---|---|---|---|---|
1 | a <- TRUE | logical | logical | logical |
2 | b <- c(1:10) | integer | numeric | integer |
3 | v1 <- as.factor(c(1:2)) | integer | numeric | factor |
4 | d <- 10 | double | numeric | numeric |
5 | e <- 10+2i | complex | complex | complex |
6 | f <- c(‘1’,‘2’) | character | character | character |
7 | v2 <- as.factor(c(‘1’,‘2’)) | integer | numeric | factor |
8 | g <- list(‘I am from Mars’) | list | list | list |
9 | h <- NULL | NULL | NULL | NULL |
10 | u <- table | closure | function | function |
11 | x <- as.Date(‘1jan1960’) | double | numeric | Date |
You may notice that the outputs of typeof()
and mode()
are not 100% the same. Good eyes! As you can see from this example, integer and double in typeof()
are returned as numeric. closure(R function) in typeof()
is returned as function.
The following codes are used to generate the above table:
library(kableExtra)
library(htmlTable)
a <- TRUE
b <- c(1:2)
v1 <- as.factor(c(1:2))
d <- 10
e <- 10+2i
f <- c('1','2')
v2 <- as.factor(c('1','2'))
g <- list('I am from Mars')
h <- NULL
u <- table ##tabulate function in R
x <- as.Date('1jan1960',"%d%b%Y")##output:"1960-01-01"
y <- matrix(c(1:4),nrow = 2)
z <- array(data = c(1:4))
my.typelist <- list(a,b,v1,d,e,f,v2,g,h,u,x)
my.examples <- list("a <- TRUE","b <- c(1:10)","v1 <- as.factor(c(1:2))",
"d <- 10","e <- 10+2i"," f <- c('1','2')","v2 <- as.factor(c('1','2'))", "g <- list('I am from Mars')","h <- NULL","u <- table","x <- as.Date('1jan1960')") %>% data.frame() %>% t()
typeof.out <- my.typelist %>% map_chr(~typeof(.))
mode.out <- my.typelist %>% map_chr(~mode(.))
class.out <- my.typelist %>% map_chr(~class(.))
tibble(my.examples,typeof.out,mode.out,class.out ) %>% htmlTable::htmlTable()
a more simpler and intuitive way to determine the type of a R object is to veiw the environment window:
Now, you can easily find the column header in the second column:)Yay! Another small tip: tibble will automatically show you the data type. see this example below:
tibble(my.examples,typeof.out,mode.out )
## # A tibble: 11 x 3
## my.examples[,1] typeof.out mode.out
## <chr> <chr> <chr>
## 1 "a <- TRUE" logical logical
## 2 "b <- c(1:10)" integer numeric
## 3 "v1 <- as.factor(c(1:2))" integer numeric
## 4 "d <- 10" double numeric
## 5 "e <- 10+2i" complex complex
## 6 " f <- c('1','2')" character character
## 7 "v2 <- as.factor(c('1','2'))" integer numeric
## 8 "g <- list('I am from Mars')" list list
## 9 "h <- NULL" NULL NULL
## 10 "u <- table" closure function
## 11 "x <- as.Date('1jan1960')" double numeric
create or test of different data types
- character
my.chr = as.character(1)
is.character(my.chr)
## [1] TRUE
- numeric
my.chr = as.numeric(1)
is.character(my.chr)
## [1] FALSE
- factor
my.chr = as.numeric()
is.character(my.chr)
## [1] FALSE