Learn R Programming

easyr (version 0.3-1)

ljoinf: Left Join with Factors

Description

dplyr's left_join doesn't work well when the data frame has factors. This function handles factors before applying left_join. Author: Bryce Chamberlain.

Usage

ljoinf(
  data.left,
  data.right,
  by,
  sort.levels = TRUE,
  restrict.levels = FALSE,
  na_level = "(Missing)",
  ...
)

Arguments

data.left

Left data. All of this data will be preservered in the join (may still result in duplication).

data.right

Right data. Only rows that matche the join will be included (may also result in duplication).

by

Columns to join on. Passed to dplyr join.

sort.levels

Sort the factor levels after combining them.

restrict.levels

Often the joined data won't use all the levels in both datasets. Set to TRUE to remove factor levels that aren't in the joined data.

na_level

dplyr doesn't like factors to have NAs so we replace NAs with this value for factors only. Set NULL to skip.

...

Other arguments passed to left_join.

Value

Joined data, with any factors modified to contain all levels in the joined data.

Examples

Run this code
# NOT RUN {
df1 = data.frame(
  factor1 = c( 'a', 'b', 'c' ),
  factor2 = c( 'high', 'medium', 'low' ),
  factor.join = c( '0349038u093843', '304359867893753', '3409783509735' ),
  numeric = c( 1, 2, 3 ),
  logical = c( TRUE, TRUE, TRUE )
)

df2 = data.frame(
  factor1 = c( 'd', 'e', 'f' ),
  factor2 = c( 'low', 'medium', 'high' ),
  factor.join = c( '32532532536', '304359867893753', '32534745876' ),
  numeric = c( 4, 5, 6 ),
  logical = c( FALSE, FALSE, FALSE )
)

ljoinf( df1, df2, by = 'factor.join' )

# }

Run the code above in your browser using DataLab