-
Notifications
You must be signed in to change notification settings - Fork 1
/
Data frames
107 lines (68 loc) · 2.24 KB
/
Data frames
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
Chapter 5
What's a data frame?
# Print out built-in R data frame
mtcars
Quick, have a look at your dataset
# Call head() on mtcars
head(mtcars)
Have a look at the structure
# Investigate the structure of mtcars
str(mtcars)
Creating a data frame
# Definition of vectors
name <- c("Mercury", "Venus", "Earth",
"Mars", "Jupiter", "Saturn",
"Uranus", "Neptune")
type <- c("Terrestrial planet",
"Terrestrial planet",
"Terrestrial planet",
"Terrestrial planet", "Gas giant",
"Gas giant", "Gas giant", "Gas giant")
diameter <- c(0.382, 0.949, 1, 0.532,
11.209, 9.449, 4.007, 3.883)
rotation <- c(58.64, -243.02, 1, 1.03,
0.41, 0.43, -0.72, 0.67)
rings <- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)
# Create a data frame from the vectors
planets_df <- data.frame(name, type, diameter, rotation, rings)
#planets_df
Creating a data frame (2)
# Check the structure of planets_df
str(planets_df)
Selection of data frame elements
# The planets_df data frame from the previous exercise is pre-loaded
planets_df
# Print out diameter of Mercury (row 1, column 3)
planets_df[1,3]
# Print out data for Mars (entire fourth row)
planets_df[4,]
Selection of data frame elements (2)
# The planets_df data frame from the previous exercise is pre-loaded
# Select first 5 values of diameter column
planets_df[1:5,"diameter"]
Only planets with rings
# planets_df is pre-loaded in your workspace
# Select the rings variable from planets_df
rings_vector <- planets_df[,"rings"]
# Print out rings_vector
rings_vector
Only planets with rings (2)
# planets_df and rings_vector are pre-loaded in your workspace
# Adapt the code to select all columns for planets with rings
planets_df[rings_vector, ]
Only planets with rings but shorter
# planets_df is pre-loaded in your workspace
# Select planets with diameter < 1
subset(planets_df, diameter < 1)
Sorting
# Play around with the order function in the console
a <- c(134567, 1567890, 3456)
order(a)
a[order(a)]
Sorting your data frame
# planets_df is pre-loaded in your workspace
# Use order() to create positions
positions <- order(planets_df$diameter)
positions
# Use positions to sort planets_df
planets_df[positions, ]