from xkcd.com
image source IKEA
join
sbind_rows()
and bind_cols()
image source commons.wikimedia.org by Jarekt
join
inner_join
, orleft_join
inner
retains only rows that find a matchleft
retains all left table rows whether they match or not
# table_to_join is the table we want to join
# the by parameter specifies which column(s)
# to base the join on
result <- input %>%
inner_join(table_to_join, by = ...)
For example
welly_pop <- welly %>%
inner_join(pop_table, by = "Meshblock")
welly_pop <- welly %>%
inner_join(pop_table, by = c("Meshblock" = "MB2013"))
mutate
to make them!
# If the types don't match, then best
# to make them match before joining
welly <- welly %>%
mutate(MB = as.character(Meshblock))
pop <- pop %>%
mutate(MB = as.character(MBnumber))
# I have made matching variables of the
# name and type, so now I don't need to
# specify, it will find the match:
welly_pop <- welly %>%
left_join(pop)
as_tibble
> as_tibble(welly)
# A tibble: 5,196 x 39
Meshblock MeshblockN AreaUnitCo AreaUnitNa UrbanAreaC UrbanAreaT UrbanAreaN
<fct> <fct> <fct> <fct> <fct> <fct> <fct>
1 MB 18850… 1885000 564022 Otaki For… 502 Rural (In… Rural (In…
2 MB 19038… 1903802 564601 Moonshine… 502 Rural (In… Rural (In…
3 MB 19247… 1924700 569202 Alicetown 018 Main Urba… Lower Hut…
4 MB 19526… 1952600 568502 Epuni East 018 Main Urba… Lower Hut…
<int>
, <dbl>
, <chr>
or <fct>
image source flickr.com/jeffsand by Jeff Sanquist
under a CC-BY-2.0 license
0
sstringr
packagecharacter
type is reliablefactor
type can do unexpected thingsdplyr
operation followed by a summarise
states <- l48 %>%
group_by(state) %>%
summarise()
states <- l48 %>%
group_by(state) %>%
summarise(across(where(is.integer), sum))