die <- 1:6
is.vector(die)
## [1] TRUE
length(die)
## [1] 6
Six types of atomic vectors: doubles, integers, characters, logicals, complex, raw.
# creating an integer and a character:
int <- c(1L, 5L)
text <- c("ace", "hearts")
sum(int)
## [1] 6
sum(text)
## Error in sum(text): invalid 'type' (character) of argument
typeof(int)
## [1] "integer"
typeof(die)
## [1] "integer"
typeof(text)
## [1] "character"
# creating complex vectors:
comp <- c(1 + 1i, 1 + 2i, 1 + 3i)
typeof(comp)
## [1] "complex"
# creating raw vectors:
raw(3)
## [1] 00 00 00
typeof(raw)
## [1] "closure"
attributes(die)
## NULL
# "names" is a very common attribute:
names(die)
## NULL
names(die) <- c("one", "two", "three", "four", "five", "six")
names(die)
## [1] "one" "two" "three" "four" "five" "six"
attributes(die)
## $names
## [1] "one" "two" "three" "four" "five" "six"
die
## one two three four five six
## 1 2 3 4 5 6
names(die) <- NULL
# "dim" turns a vector into an n-dimensional array:
dim(die) <- c(2,3)
die
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
# even a hypercube with 1 by 2 by 3 dimensions:
dim(die) <- c(1, 2, 3)
die
## , , 1
##
## [,1] [,2]
## [1,] 1 2
##
## , , 2
##
## [,1] [,2]
## [1,] 3 4
##
## , , 3
##
## [,1] [,2]
## [1,] 5 6
m <- matrix(die, nrow = 2)
m
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
# to fill up the matrix row by row:
m <- matrix(die, nrow = 2, byrow = TRUE)
m
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
ar <- array(c(11:14, 21:24, 31:34), dim = c(2, 2, 3))
ar
## , , 1
##
## [,1] [,2]
## [1,] 11 13
## [2,] 12 14
##
## , , 2
##
## [,1] [,2]
## [1,] 21 23
## [2,] 22 24
##
## , , 3
##
## [,1] [,2]
## [1,] 31 33
## [2,] 32 34
Changing the dimensions of my object don’t change the type of the object, but it changes the class of the object.
dim(die) <- c(2,3)
typeof(die)
## [1] "integer"
class(die)
## [1] "matrix"
class("Hello")
## [1] "character"
class(4)
## [1] "numeric"
# data type and class of time:
now <- Sys.time()
now
## [1] "2020-05-22 22:09:42 EDT"
typeof(now)
## [1] "double"
class(now)
## [1] "POSIXct" "POSIXt"
# POSIXct counts the time since 12:00AM January 1st 1970. To see this time:
unclass(now)
## [1] 1590199782
# what day it was a million second after 12:00AM Jan 1st 1970?:
mil <- 1000000
mil
## [1] 1e+06
class(mil) <- c("POSIXct", "POSIXt")
mil
## [1] "1970-01-12 08:46:40 EST"
R’s way of storing categorical variables.
# creating a factor:
gender <- factor(c("male", "female", "male"))
typeof(gender)
## [1] "integer"
attributes(gender)
## $levels
## [1] "female" "male"
##
## $class
## [1] "factor"
unclass(gender)
## [1] 2 1 2
## attr(,"levels")
## [1] "female" "male"
gender
## [1] male female male
## Levels: female male
# factor makes the categorical info coded as numbers
# factor to character:
as.character(gender)
## [1] "male" "female" "male"
Figure 1: R’s Coercion criteria
# coercion of logical vector:
logi <- c(T, F, T, F, T)
sum(logi)
## [1] 3
mean(logi) # proportion of TRUE's
## [1] 0.6
# convert data from one type to another:
as.character(1)
## [1] "1"
as.logical(2)
## [1] TRUE
as.numeric(FALSE)
## [1] 0
They are used to group together R objects.
list1 <- list(100:120, "R", list(T, F))
list1
## [[1]]
## [1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
## [20] 119 120
##
## [[2]]
## [1] "R"
##
## [[3]]
## [[3]][[1]]
## [1] TRUE
##
## [[3]][[2]]
## [1] FALSE
# double-brackets show elements while single brackets show subelements.
Figure 2: Data Frame format
df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"), value = c(1,2,3))
df
## face suit value
## 1 ace clubs 1
## 2 two clubs 2
## 3 six clubs 3
typeof(df)
## [1] "list"
class(df)
## [1] "data.frame"
str(df)
## 'data.frame': 3 obs. of 3 variables:
## $ face : Factor w/ 3 levels "ace","six","two": 1 3 2
## $ suit : Factor w/ 1 level "clubs": 1 1 1
## $ value: num 1 2 3
# to keep characters as characters instead of turning them into factors:
df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"), value = c(1,2,3),
stringsAsFactors = FALSE)
deck <- read.table("deck.csv")
head(deck)
## V1 V2
## 1 face ,"suit","value"
## 2 king ,"spades",13
## 3 queen ,"spades",12
## 4 jack ,"spades",11
## 5 ten ,"spades",10
## 6 nine ,"spades",9
tail(deck)
## V1 V2
## 48 six ,"hearts",6
## 49 five ,"hearts",5
## 50 four ,"hearts",4
## 51 three ,"hearts",3
## 52 two ,"hearts",2
## 53 ace ,"hearts",1
write.csv(deck, file = "cards.csv", row.names = F)
# always use rown.names=FALSE
# to see my working directory:
getwd()
## [1] "C:/Users/HP/Dropbox/USA/Academics Complementary/Language and Softs/R/Hands on programming with R"
Figure 3: Common R data types