-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path04-ModifyingDataFrame.R
More file actions
77 lines (44 loc) · 1.9 KB
/
04-ModifyingDataFrame.R
File metadata and controls
77 lines (44 loc) · 1.9 KB
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
# load libraries
library(readr)
library(dplyr)
# load data frame
dogs <- read_csv('dogs.csv')
# inspect data frame
head(dogs)
# add average height column
dogs <- dogs %>%
mutate(avg_height = (height_low_inches + height_high_inches)/2)
head(dogs)
# add average height, average weight and rank change columns.
dogs <- dogs %>%
mutate(avg_height = (height_low_inches + height_high_inches)/2)
dogs <- dogs %>%
mutate(avg_weight = (weight_low_lbs + weight_high_lbs)/2, rank_change_13_to_16 = rank_2016 - rank_2013)
head(dogs)
# Update the code in the last code block to add the columns avg_height, avg_weight, and rank_change_13_to_16 to dogs while dropping all existing columns.
dogs <- dogs %>%
transmute(avg_height = (height_low_inches + height_high_inches)/2, avg_weight = (weight_low_lbs + weight_high_lbs)/2,
rank_change_13_to_16 = rank_2016 - rank_2013)
head(dogs)
# check column names
original_col_names <- colnames(dogs)
# rename data frame columns
dogs <- dogs %>%
rename(avg_height_inches = avg_height,
avg_weight_lbs = avg_weight,
popularity_change_13_to_16 = rank_change_13_to_16)
# check column names
new_col_names <- colnames(dogs)
new_col_names
# Add the two columns defined below to dogs while dropping all existing columns from the data frame:
# height_average_feet is the average of height_low_inches and height_high_inches divided by 12, since there are 12 inches in 1 foot.
# popularity_change_15_to_16 is the difference in rank from rank_2016 to rank_2015.
dogs <- dogs %>%
transmute(height_average_feet = ((height_low_inches + height_high_inches)/2)/12, popularity_change_15_to_16 = rank_2016 - rank_2015)
head(dogs)
dogs <- dogs %>%
transmute(breed = breed,
height_average_feet = ((height_low_inches + height_high_inches)/2)/12,
popularity_change_15_to_16 = rank_2016 - rank_2015) %>%
arrange(desc(popularity_change_15_to_16))
head(dogs)