0% found this document useful (0 votes)
32 views8 pages

Ch1 2 Basic Data Types

This document introduces basic data types in R including logical, numeric, integer, character, and other atomic types such as double, complex, and raw. It demonstrates how to check the class of different values and coerce between types, noting that integer is a subset of numeric and coercion from character to numeric can introduce NAs.

Uploaded by

binzbinz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views8 pages

Ch1 2 Basic Data Types

This document introduces basic data types in R including logical, numeric, integer, character, and other atomic types such as double, complex, and raw. It demonstrates how to check the class of different values and coerce between types, noting that integer is a subset of numeric and coercion from character to numeric can introduce NAs.

Uploaded by

binzbinz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

INTRODUCTION TO R

Basic Data Types

Introduction to R

logical
> TRUE
[1] TRUE
> class(TRUE)
[1] "logical"
> FALSE
[1] FALSE
> class(NA)
[1] "logical"
> T
[1] TRUE
> F
[1] FALSE

class() to reveal type

Introduction to R

numeric
> 2
[1] 2
> 2.5
[1] 2.5
> 2L
[1] 2
> class(2)
[1] "numeric"
> class(2L)
[1] "integer"

Introduction to R

numeric
> is.numeric(2)
[1] TRUE
> is.numeric(2L)
[1] TRUE
> is.integer(2)
[1] FALSE
> is.integer(2L)
[1] TRUE

integer is numeric
numeric not always integer

Introduction to R

character
> "I love data science!"
[1] "I love data science!"
> class("I love data science!")
[1] "character"

Introduction to R

Other atomic types

double: higher precision

complex: complex numbers

raw: store raw bytes

Introduction to R

Coercion
> as.numeric(TRUE)
[1] 1
> as.numeric(FALSE)
[1] 0
> as.character(4)
[1] "4"
> as.numeric("4.5")
[1] 4.5
> as.integer("4.5")
[1] 4
> as.numeric("Hello")
[1] NA
Warning message:
NAs introduced by coercion

INTRODUCTION TO R

Lets practice!

You might also like