# Read your local CSV file directly from your working directory
volcano_data <- read_csv("apply it to your data-2(MyData).csv", show_col_types = FALSE)
## New names:
## • `volcano_number` -> `volcano_number...1`
## • `` -> `...13`
## • `` -> `...19`
## • `volcano_number` -> `volcano_number...20`
## • `volcano_number` -> `volcano_number...21`
## • `volcano_number` -> `volcano_number...22`
## • `volcano_number` -> `volcano_number...27`
## • `` -> `...28`
## • `` -> `...29`
## • `` -> `...30`
## • `` -> `...31`
## • `` -> `...32`
## • `` -> `...33`
## • `` -> `...34`
## • `` -> `...35`
# Select a few columns to verify it loaded correctly
small_volcano <- volcano_data %>%
select(volcano_name, country, elevation) %>%
head(3)
small_volcano
## # A tibble: 3 × 3
## volcano_name country elevation
## <chr> <chr> <dbl>
## 1 Abu Japan 641
## 2 Acamarachi Chile 6023
## 3 Acatenango Guatemala 3976
# Transform long data into wide form using pivot_wider
# Using the built-in table2 dataset as demonstrated in course materials
table2
## # A tibble: 12 × 4
## country year type count
## <chr> <dbl> <chr> <dbl>
## 1 Afghanistan 1999 cases 745
## 2 Afghanistan 1999 population 19987071
## 3 Afghanistan 2000 cases 2666
## 4 Afghanistan 2000 population 20595360
## 5 Brazil 1999 cases 37737
## 6 Brazil 1999 population 172006362
## 7 Brazil 2000 cases 80488
## 8 Brazil 2000 population 174504898
## 9 China 1999 cases 212258
## 10 China 1999 population 1272915272
## 11 China 2000 cases 213766
## 12 China 2000 population 1280428583
table2_wide <- table2 %>%
pivot_wider(names_from = type, values_from = count)
table2_wide
## # A tibble: 6 × 4
## country year cases population
## <chr> <dbl> <dbl> <dbl>
## 1 Afghanistan 1999 745 19987071
## 2 Afghanistan 2000 2666 20595360
## 3 Brazil 1999 37737 172006362
## 4 Brazil 2000 80488 174504898
## 5 China 1999 212258 1272915272
## 6 China 2000 213766 1280428583
#wide to long form
# Transform wide data into long form using pivot_longer
# Using table4a directly from the course materials
table4a
## # A tibble: 3 × 3
## country `1999` `2000`
## <chr> <dbl> <dbl>
## 1 Afghanistan 745 2666
## 2 Brazil 37737 80488
## 3 China 212258 213766
table4a_long <- table4a %>%
pivot_longer(cols = c(`1999`, `2000`), names_to = "year", values_to = "cases")
table4a_long
## # A tibble: 6 × 3
## country year cases
## <chr> <chr> <dbl>
## 1 Afghanistan 1999 745
## 2 Afghanistan 2000 2666
## 3 Brazil 1999 37737
## 4 Brazil 2000 80488
## 5 China 1999 212258
## 6 China 2000 213766
#separate a column
# Using separate() from tidyr to split a single column into multiple columns
# Using table3 dataset example where rate contains cases and population separated by '/'
table3
## # A tibble: 6 × 3
## country year rate
## <chr> <dbl> <chr>
## 1 Afghanistan 1999 745/19987071
## 2 Afghanistan 2000 2666/20595360
## 3 Brazil 1999 37737/172006362
## 4 Brazil 2000 80488/174504898
## 5 China 1999 212258/1272915272
## 6 China 2000 213766/1280428583
table3_separated <- table3 %>%
separate(rate, into = c("cases", "population"), sep = "/")
table3_separated
## # A tibble: 6 × 4
## country year cases population
## <chr> <dbl> <chr> <chr>
## 1 Afghanistan 1999 745 19987071
## 2 Afghanistan 2000 2666 20595360
## 3 Brazil 1999 37737 172006362
## 4 Brazil 2000 80488 174504898
## 5 China 1999 212258 1272915272
## 6 China 2000 213766 1280428583
#Unite two columns
# Using unite() from tidyr to combine two columns back together
table3_united <- table3_separated %>%
unite(col = "rate", cases, population, sep = "/")
table3_united
## # A tibble: 6 × 3
## country year rate
## <chr> <dbl> <chr>
## 1 Afghanistan 1999 745/19987071
## 2 Afghanistan 2000 2666/20595360
## 3 Brazil 1999 37737/172006362
## 4 Brazil 2000 80488/174504898
## 5 China 1999 212258/1272915272
## 6 China 2000 213766/1280428583
#Missing Values
# Handling explicit and implicit missing values using tidyr functions
# Example using stocks dataset from the course materials
stocks <- tibble(
year = c(2015, 2015, 2015, 2015, 2016, 2016, 2016),
qtr = c( 1, 2, 3, 4, 2, 3, 4),
return = c(1.88, 0.59, 0.35, NA, 0.92, 0.17, 2.66)
)
stocks
## # A tibble: 7 × 3
## year qtr return
## <dbl> <dbl> <dbl>
## 1 2015 1 1.88
## 2 2015 2 0.59
## 3 2015 3 0.35
## 4 2015 4 NA
## 5 2016 2 0.92
## 6 2016 3 0.17
## 7 2016 4 2.66
# Making implicit missing values explicit by pivoting wider
stocks_wide <- stocks %>%
pivot_wider(names_from = year, values_from = return)
stocks_wide
## # A tibble: 4 × 3
## qtr `2015` `2016`
## <dbl> <dbl> <dbl>
## 1 1 1.88 NA
## 2 2 0.59 0.92
## 3 3 0.35 0.17
## 4 4 NA 2.66
# Filling missing values downwards using fill()
treatment <- tibble(
person = c("Kirsty", NA, NA, "Ben"),
treatment = c(1, 2, 1, 2),
response = c(4, 5, 1, 3)
)
treatment %>% fill(person, .direction = "down")
## # A tibble: 4 × 3
## person treatment response
## <chr> <dbl> <dbl>
## 1 Kirsty 1 4
## 2 Kirsty 2 5
## 3 Kirsty 1 1
## 4 Ben 2 3