Skip to content

Binding

Whereas joins are useful for combining data frames based on matching keys, another way to combine data frames is to bind them together, which can be done either by rows or by columns. TidierData.jl implements these actions using @bind_rows() and @bind_cols(), respectively.

Let's generate three data frames to combine.

using TidierData

df1 = DataFrame(a=1:3, b=1:3);

df2 = DataFrame(a=4:6, b=4:6);

df3 = DataFrame(a=7:9, c=7:9);

@bind_rows()¤

@bind_rows(df1, df2)
6×2 DataFrame
Rowab
Int64Int64
111
222
333
444
555
666

@bind_rows() keeps columns that are present in at least one of the provided data frames. Any missing columns will be filled with missing values.

@bind_rows(df1, df3)
6×3 DataFrame
Rowabc
Int64Int64?Int64?
111missing
222missing
333missing
47missing7
58missing8
69missing9

There is an optional id argument to add an identifier for combined data frames. Note that both @bind_rows and @bind_cols accept multiple (i.e., more than 2) data frames, as in the example below.

@bind_rows(df1, df2, df3, id = "id")
9×4 DataFrame
Rowabcid
Int64Int64?Int64?Int64
111missing1
222missing1
333missing1
444missing2
555missing2
666missing2
77missing73
88missing83
99missing93

@bind_cols()¤

@bind_cols works similarly to R's tidyverse although the .name_repair argument is not supported.

@bind_cols(df1, df2)
3×4 DataFrame
Rowaba_1b_1
Int64Int64Int64Int64
11144
22255
33366

This page was generated using Literate.jl.