-
Notifications
You must be signed in to change notification settings - Fork 10
/
DataFormats.R
137 lines (91 loc) · 2.47 KB
/
DataFormats.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File: DataFormats.R
# Course: R: An Introduction (with RStudio)
# DATA TYPES ###############################################
# Numeric
n1 <- 15 # Double precision by default
n1
typeof(n1)
n2 <- 1.5
n2
typeof(n2)
# Character
c1 <- "c"
c1
typeof(c1)
c2 <- "a string of text"
c2
typeof(c2)
# Logical
l1 <- TRUE
l1
typeof(l1)
l2 <- F
l2
typeof(l2)
# DATA STRUCTURES ##########################################
## Vector ##################################################
v1 <- c(1, 2, 3, 4, 5)
v1
is.vector(v1)
v2 <- c("a", "b", "c")
v2
is.vector(v2)
v3 <- c(TRUE, TRUE, FALSE, FALSE, TRUE)
v3
is.vector(v3)
## Matrix ##################################################
m1 <- matrix(c(T, T, F, F, T, F), nrow = 2)
m1
m2 <- matrix(c("a", "b",
"c", "d"),
nrow = 2,
byrow = T)
m2
## Array ###################################################
# Give data, then dimemensions (rows, columns, tables)
a1 <- array(c( 1:24), c(4, 3, 2))
a1
## Data frame ##############################################
# Can combine vectors of the same length
vNumeric <- c(1, 2, 3)
vCharacter <- c("a", "b", "c")
vLogical <- c(T, F, T)
dfa <- cbind(vNumeric, vCharacter, vLogical)
dfa # Matrix of one data type
df <- as.data.frame(cbind(vNumeric, vCharacter, vLogical))
df # Makes a data frame with three different data types
## List ####################################################
o1 <- c(1, 2, 3)
o2 <- c("a", "b", "c", "d")
o3 <- c(T, F, T, T, F)
list1 <- list(o1, o2, o3)
list1
list2 <- list(o1, o2, o3, list1) # Lists within lists!
list2
# COERCING TYPES ###########################################
## Automatic coercion ######################################
# Goes to "least restrictive" data type
(coerce1 <- c(1, "b", TRUE))
# coerce1 # Parenthese around command above make this moot
typeof(coerce1)
## Coerce numeric to integer ###############################
(coerce2 <- 5)
typeof(coerce2)
(coerce3 <- as.integer(5))
typeof(coerce3)
## Coerce character to numeric #############################
(coerce4 <- c("1", "2", "3"))
typeof(coerce4)
(coerce5 <- as.numeric(c("1", "2", "3")))
typeof(coerce5)
## Coerce matrix to data frame #############################
(coerce6 <- matrix(1:9, nrow= 3))
is.matrix(coerce6)
(coerce7 <- as.data.frame(matrix(1:9, nrow= 3)))
is.data.frame(coerce7)
# CLEAN UP #################################################
# Clear environment
rm(list = ls())
# Clear console
cat("\014") # ctrl+L
# Clear mind :)