-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata-types-demo.R
100 lines (64 loc) · 2.11 KB
/
data-types-demo.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
# =============================================================================
# title: R Workshop Demo: Data Types in R
# author: Nura Kawa
# summary: A quick demonstration of reading/writing csv, text files.
# Loading data from web.
# data:
# =============================================================================
# Vectors
# =============================================================================
instructors <- c("Adnan", "Vinitra", "Nura")
instructors
class(instructors) # character
vec <- 1:40 # numeric
sum(vec); mean(vec); mode(vec)
mixed <- c("cat", TRUE, "llama", NA, 11:15)
mixed
sum(mixed) # Error: invalid 'type' of argument
v <- rnorm(10000, 0, 1)
hist(v, col = "tomato")
# Lists
# =============================================================================
lst <- list(sin, 2:4, "sine", list(1, list(2)))
lst
lst[[1]](pi/2) # sin(pi/2)
# Matrices
# =============================================================================
mat <- matrix(c("apple", "orange", "banana"),
nrow = 3,
ncol = 3,
byrow = FALSE) # I want to make column vectors
mat
A = matrix(1:25,
nrow = 5,
ncol = 5)
t(A) # transpose A
A %*% t(A) # A * (A transpose)
# Arrays
# =============================================================================
arr <- array(NA,dim = c(3, 3, 2))
# Factors
# =============================================================================
data(iris)
iris[sample(nrow(iris), 6),]
class(iris$Species)
levels(iris$Species)
# Data Frames
# =============================================================================
head(iris)
class(iris)
str(iris)
df <- data.frame("count" = 1:11,
"height" = seq(5.0, 6.0, by=0.1))
# Subsetting
# =============================================================================
# using the $
iris$Sepal.Width
# using a logical
logical_vector <- iris$Sepal.Width > 3.5
iris[logical_vector,]
# using []
iris[1:4, 1:2] # select rows 1 to 4, columns 1 to 2
# using [] and "name"
iris[1:10, "Sepal.Width"] # the first ten elements of the vector
# Sepal.Width