-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnit 4 Live Session R Code.RMD
29 lines (16 loc) · 1.44 KB
/
Unit 4 Live Session R Code.RMD
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
# Unit 4 Live Session R Code
#Question 1
# Dataframe Arranged But Plot Not Ordered
FIFA %>% filter(!is.na(BallControl)) %>% group_by(Position) %>% summarize(meanBC = mean(BallControl),count = n()) %>% arrange(meanBC)
FIFA %>% filter(!is.na(BallControl)) %>% group_by(Position) %>% summarize(meanBC = mean(BallControl),count = n()) %>% arrange(meanBC) %>% ggplot(aes(x = Position, y = meanBC)) + geom_col()
# Data Frame arranged and Plot Ordered (arrange here is reducndent)
FIFA %>% filter(!is.na(BallControl)) %>% group_by(Position) %>% summarize(meanBC = mean(BallControl),count = n()) %>% arrange(meanBC) %>% ggplot(aes(x = factor(Position, levels = Position[order(meanBC)]), y = meanBC)) + geom_col()
# Data Frame not arranged but Plot Ordered
FIFA %>% filter(!is.na(BallControl)) %>% group_by(Position) %>% summarize(meanBC = mean(BallControl),count = n()) %>% ggplot(aes(x = factor(Position, levels = Position[order(meanBC)]), y = meanBC)) + geom_col()
#Question 2
#Dropping Unused Levels from a Factor using droplevels
FIFA %>% select(Agility,Acceleration,Position) %>% filter(Position == "LM" | Position == "LF") %>% droplevels() %>% ggpairs(aes(color = Position))
# Hack ... create new variable with only those levels
FIFA2 = FIFA %>% filter(Position == "LM" | Position == "LF")
FIFA2$Position = as.factor(as.character(FIFA2$Position))
FIFA2 %>% select(Agility,Acceleration,Position) %>% ggpairs(aes(color = Position))