left_join_fill: join_left_fill

Description Usage Arguments Value Examples

View source: R/wagnertimoR.R

Description

This function proceeds a left join (dplyr packages) and fills missing values of the 'by variable' with a given value (instead of the default NA). Importantly, the other valid NA values are preserved. BUT only for the data.frame on the left side! NOTE: dplyr and tidyr package are needed!

Usage

1
left_join_fill(x, y, by, fill = 0)

Arguments

x

- the left data.frame where the NAs are perserved

y

- the right data.frame wherre NA values can occur due to the join. CAUTION valid NAs get also replaced by the fill value!

by

- the by variable to join the data.frames

fill

- custom fill value to replace the default NA in dplyrs join function

Value

the joined data.frame with filled 0 values

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
dt1 <- data.table(x=c('a', 'b', 'c', 'd', 'e'), y=c(NA, 'w', NA, 'y', 'z'));
dt2 <- data.table(x=c('a', 'b', 'c'), new_col=c(1,2,3));

left_join_fill(dt1, dt2, by = x, fill = 0)

#  x    y new_col
#1 a <NA>       1
#2 b    w       2
#3 c <NA>       3
#4 d    y       0
#5 e    z       0

wagnertimo/wagnertimoR documentation built on May 3, 2019, 7:37 p.m.