-
Notifications
You must be signed in to change notification settings - Fork 10
/
Factors.R
60 lines (45 loc) · 1.14 KB
/
Factors.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
# File: Factors.R
# Course: R: An Introduction (with RStudio)
# CREATE DATA ##############################################
(x1 <- 1:3)
(y <- 1:9)
# Combine variables
(df1 <- cbind.data.frame(x1, y))
typeof(df1$x1)
str(df1)
# AS.FACTOR ################################################
(x2 <- as.factor(c(1:3)))
(df2 <- cbind.data.frame(x2, y))
typeof(df2$x2)
str(df2)
# DEFINE EXISTING VARIABLE AS FACTOR #######################
x3 <- c(1:3)
df3 <- cbind.data.frame(x3, y)
(df3$x3 <- factor(df3$x3,
levels = c(1, 2, 3)))
typeof(df3$x3)
str(df3)
# LABELS FOR FACTOR ########################################
x4 <- c(1:3)
df4 <- cbind.data.frame(x4, y)
df4$x4 <- factor(df4$x4,
levels = c(1, 2, 3),
labels = c("macOS", "Windows", "Linux"))
df4
typeof(df4$x4)
str(df4)
# ORDERED FACTORS AND LABELS ###############################
x5 <- c(1:3)
df5 <- cbind.data.frame(x5, y)
(df5$x5 <- ordered(df5$x5,
levels = c(3, 1, 2),
labels = c("No", "Maybe", "Yes")))
df5
typeof(df5$x5)
str(df5)
# CLEAN UP #################################################
# Clear environment
rm(list = ls())
# Clear console
cat("\014") # ctrl+L
# Clear mind :)