Tidy Data

  • Each column = one variable (convention is to have “fixed” columns first)

  • Each row = one observation

  • Each cell = one value

[1]:
library(tidyverse)
Registered S3 methods overwritten by 'ggplot2':
  method         from
  [.quosures     rlang
  c.quosures     rlang
  print.quosures rlang
── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──
 ggplot2 3.1.1      purrr   0.3.2
 tibble  2.1.2      dplyr   0.8.1
 tidyr   0.8.3      stringr 1.4.0
 readr   1.3.1      forcats 0.4.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
 dplyr::filter() masks stats::filter()
 dplyr::lag()    masks stats::lag()

Data set

[2]:
n <- 4
df <- data.frame(
    pid = c(1,3,4,5),
    desc = paste(sample(c('M', 'F'), n, replace=T),
                    '-',
                    sample(10:70, n),
                    sep=''),
    visit1 = rpois(n, lambda = 20),
    visit2 = rpois(n, lambda=10)
)
[3]:
df[3,3] = NA
[4]:
df
A data.frame: 4 × 4
piddescvisit1visit2
<dbl><fct><int><int>
1F-2017 6
3F-6124 9
4F-48NA10
5M-571411

Gather

[5]:
df %>%
gather(visit, measurement, -pid, -desc)
A data.frame: 8 × 4
piddescvisitmeasurement
<dbl><fct><chr><int>
1F-20visit117
3F-61visit124
4F-48visit1NA
5M-57visit114
1F-20visit2 6
3F-61visit2 9
4F-48visit210
5M-57visit211
[6]:
df %>%
gather(key=visit, value=measurement, visit1:visit2)
A data.frame: 8 × 4
piddescvisitmeasurement
<dbl><fct><chr><int>
1F-20visit117
3F-61visit124
4F-48visit1NA
5M-57visit114
1F-20visit2 6
3F-61visit2 9
4F-48visit210
5M-57visit211

Separate

[7]:
df %>%
gather(key=visit, value=measurement, visit1:visit2) %>%
separate(desc, sep='-', into=c("sex", "age"))
A data.frame: 8 × 5
pidsexagevisitmeasurement
<dbl><chr><chr><chr><int>
1F20visit117
3F61visit124
4F48visit1NA
5M57visit114
1F20visit2 6
3F61visit2 9
4F48visit210
5M57visit211

Clean-up and type coercion

[8]:
df %>%
gather(key=visit, value=measurement, visit1:visit2) %>%
separate(desc, sep='-', into=c("sex", "age")) %>%
mutate(age=as.integer(age),
       visit=str_remove(visit, "visit"),
       visit=as.integer(visit)) %>%
drop_na(measurement) -> df1
[9]:
df1
A data.frame: 7 × 5
pidsexagevisitmeasurement
<dbl><chr><int><int><int>
11F20117
23F61124
45M57114
51F202 6
63F612 9
74F48210
85M57211

Joins

[10]:
names <- data.frame(
    pid = 1:6,
    first = c( "bob", "dan","ann", "liz", "joe", "jen"),
    last = c("lim", "tan", "liu", "nguyn", "smith", "finkelstein")
)
[11]:
names
A data.frame: 6 × 3
pidfirstlast
<int><fct><fct>
1boblim
2dantan
3annliu
4liznguyn
5joesmith
6jenfinkelstein
[12]:
inner_join(df, names, by = "pid")
A data.frame: 4 × 6
piddescvisit1visit2firstlast
<dbl><fct><int><int><fct><fct>
1F-2017 6boblim
3F-6124 9annliu
4F-48NA10liznguyn
5M-571411joesmith
[13]:
left_join(df, names, by = "pid")
A data.frame: 4 × 6
piddescvisit1visit2firstlast
<dbl><fct><int><int><fct><fct>
1F-2017 6boblim
3F-6124 9annliu
4F-48NA10liznguyn
5M-571411joesmith
[14]:
right_join(df, names, by = "pid")
A data.frame: 6 × 6
piddescvisit1visit2firstlast
<dbl><fct><int><int><fct><fct>
1F-2017 6boblim
2NA NANAdantan
3F-6124 9annliu
4F-48NA10liznguyn
5M-571411joesmith
6NA NANAjenfinkelstein
[15]:
full_join(df, names, by = "pid")
A data.frame: 6 × 6
piddescvisit1visit2firstlast
<dbl><fct><int><int><fct><fct>
1F-2017 6boblim
3F-6124 9annliu
4F-48NA10liznguyn
5M-571411joesmith
2NA NANAdantan
6NA NANAjenfinkelstein

Exercise

1. Using the who data set, summarize the total count for each method of TB diagnosis across all years for which there is data for countries that begin wiht ‘Z’.

[16]:
help(who)
[17]:
who %>% sample_n(5)
A tibble: 5 × 60
countryiso2iso3yearnew_sp_m014new_sp_m1524new_sp_m2534new_sp_m3544new_sp_m4554new_sp_m5564newrel_m4554newrel_m5564newrel_m65newrel_f014newrel_f1524newrel_f2534newrel_f3544newrel_f4554newrel_f5564newrel_f65
<chr><chr><chr><int><int><int><int><int><int><int><int><int><int><int><int><int><int><int><int><int>
Iceland ISISL1990NANANANANANANANANANANANANANANANA
West Bank and Gaza StripPSPSE2006 0 1 3 4 1 1NANANANANANANANANANA
Vanuatu VUVUT2006 1 5 3 1 4 4NANANANANANANANANANA
Israel ILISR1988NANANANANANANANANANANANANANANANA
Senegal SNSEN1982NANANANANANANANANANANANANANANANA
[18]:
colnames(who)
  1. 'country'
  2. 'iso2'
  3. 'iso3'
  4. 'year'
  5. 'new_sp_m014'
  6. 'new_sp_m1524'
  7. 'new_sp_m2534'
  8. 'new_sp_m3544'
  9. 'new_sp_m4554'
  10. 'new_sp_m5564'
  11. 'new_sp_m65'
  12. 'new_sp_f014'
  13. 'new_sp_f1524'
  14. 'new_sp_f2534'
  15. 'new_sp_f3544'
  16. 'new_sp_f4554'
  17. 'new_sp_f5564'
  18. 'new_sp_f65'
  19. 'new_sn_m014'
  20. 'new_sn_m1524'
  21. 'new_sn_m2534'
  22. 'new_sn_m3544'
  23. 'new_sn_m4554'
  24. 'new_sn_m5564'
  25. 'new_sn_m65'
  26. 'new_sn_f014'
  27. 'new_sn_f1524'
  28. 'new_sn_f2534'
  29. 'new_sn_f3544'
  30. 'new_sn_f4554'
  31. 'new_sn_f5564'
  32. 'new_sn_f65'
  33. 'new_ep_m014'
  34. 'new_ep_m1524'
  35. 'new_ep_m2534'
  36. 'new_ep_m3544'
  37. 'new_ep_m4554'
  38. 'new_ep_m5564'
  39. 'new_ep_m65'
  40. 'new_ep_f014'
  41. 'new_ep_f1524'
  42. 'new_ep_f2534'
  43. 'new_ep_f3544'
  44. 'new_ep_f4554'
  45. 'new_ep_f5564'
  46. 'new_ep_f65'
  47. 'newrel_m014'
  48. 'newrel_m1524'
  49. 'newrel_m2534'
  50. 'newrel_m3544'
  51. 'newrel_m4554'
  52. 'newrel_m5564'
  53. 'newrel_m65'
  54. 'newrel_f014'
  55. 'newrel_f1524'
  56. 'newrel_f2534'
  57. 'newrel_f3544'
  58. 'newrel_f4554'
  59. 'newrel_f5564'
  60. 'newrel_f65'
[19]:
who %>%
select(-iso2, -iso3) %>%
gather(key=group, value=n, starts_with('new')) -> who1
[20]:
who1 %>% head
A tibble: 6 × 4
countryyeargroupn
<chr><int><chr><int>
Afghanistan1980new_sp_m014NA
Afghanistan1981new_sp_m014NA
Afghanistan1982new_sp_m014NA
Afghanistan1983new_sp_m014NA
Afghanistan1984new_sp_m014NA
Afghanistan1985new_sp_m014NA
[21]:
who1 %>%
mutate(group = str_replace(group, "newrel", "new_rel")) -> who2
[22]:
who2 %>% head
A tibble: 6 × 4
countryyeargroupn
<chr><int><chr><int>
Afghanistan1980new_sp_m014NA
Afghanistan1981new_sp_m014NA
Afghanistan1982new_sp_m014NA
Afghanistan1983new_sp_m014NA
Afghanistan1984new_sp_m014NA
Afghanistan1985new_sp_m014NA
[23]:
who2 %>%
separate(group, sep="_", into=c("type", "method", "age_group")) -> who3
[24]:
who3 %>% head
A tibble: 6 × 6
countryyeartypemethodage_groupn
<chr><int><chr><chr><chr><int>
Afghanistan1980newspm014NA
Afghanistan1981newspm014NA
Afghanistan1982newspm014NA
Afghanistan1983newspm014NA
Afghanistan1984newspm014NA
Afghanistan1985newspm014NA
[25]:
who3 %>%
drop_na(n) -> who4
[26]:
who4 %>% head
A tibble: 6 × 6
countryyeartypemethodage_groupn
<chr><int><chr><chr><chr><int>
Afghanistan1997newspm014 0
Afghanistan1998newspm014 30
Afghanistan1999newspm014 8
Afghanistan2000newspm014 52
Afghanistan2001newspm014129
Afghanistan2002newspm014 90
[28]:
who4 %>%
   filter(str_detect(country, '^Z')) %>%
   group_by(country, method) %>%
   summarize(count=sum(n))
A grouped_df: 8 × 3
countrymethodcount
<chr><chr><int>
Zambia ep 19082
Zambia rel 40638
Zambia sn 37054
Zambia sp 167064
Zimbabweep 41719
Zimbabwerel 32899
Zimbabwesn 152573
Zimbabwesp 133224