forked from INFO-498F/lecture-6-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise2.R
More file actions
68 lines (45 loc) · 2.04 KB
/
exercise2.R
File metadata and controls
68 lines (45 loc) · 2.04 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
### Exercise 2 ###
# Load R's "USPersonalExpenditure" dataest using the "data()" function
data("USPersonalExpenditure")
View(expenditure)
# The variable USPersonalExpenditure is now accessible to you. Unfortunately, it's not a data.frame
# Test this using the is.data.frame function
is.data.frame(expenditure)
# Luckily, you can simply pass the USPersonalExpenditure variable to the data.frame function
# to convert it a data.farme
# Create a new variable by passing the USPersonalExpenditure to the data.frame function
us_exp <- data.frame(USPersonalExpenditure)
View(us_exp)
# What are the column names of your dataframe?
name_of_columns <- colnames(us_exp)
# Why are they so strange?
# Not sure
# What are the row names of your dataframe?
name_of_rows <- rownames(us_exp)
# Create a column "category" that is equal to your rownames
us_exp$category <- name_of_rows
# Store all "Medical and Health" expenditures in a variable
medical_health <- us_exp['Medical and Health',]
# Store only your 1955 data in a varialbe -- what type of variable does it become?
spending_1955 <- us_exp['X1955']
# Oh no! It's a vector! repeat the same selection above, but:
# include "drop=FALSE" in your square brackets to keep the rownames
# How much money was spent on personal care in 1940?
spending_1940_personal <- us_exp['Personal Care', 'X1940']
# How much money was spent on Food and Tobacco in 1960
# What was the highest expenditure category in 1960?
highest_1960 <- us_exp$category[us_exp$X1960 == max(us_exp$X1960)]
# Write a function that takes in a year as a parameter, and
# returns the highest spending category of that year
detect_highest <- function(year) {
return(us_exp$category[us_exp[year] == max(us_exp[year])])
}
# Using your function, determine the highest spending category of each year
### Bonus ###
# Write a loop to cycle through the years, and store the highest spending category of
# each year in a list
highest <- list()
for(year in seq(1940, 1960, 5)) {
year_index <- paste0('X', year)
highest[year_index] <- detect_highest(year_index)
}