Function to duplicate values in column x from a wide-form (one row per case) data set wf based on a shared column y with a long-form (multiple rows per case) data set lf.

duplicate_wide_to_long(wf, lf, x, y, default = NA)

Arguments

wf

A wide-form data frame.

lf

A long-form data frame.

x

The column in wf with values to duplicate (non-standard evaluation possible).

y

The column in both wf and lf over which to repeat values over (non-standard evaluation possible).

default

The value to substitute if no cases for x based on y are found to duplicate.

Value

A vector matching in length to the number of rows of lf with the values of x

repeated for each unique case of y.

Examples

# Example wide-form data-frame
wf <- data.frame(
  ID = 1:3,
  Value = 4:6
)

# Example long-form data-frame
lf <- data.frame(
  ID = rep( 1:3, each = 3),
  Value = NA
)

# Duplicate values from 'wf' based
# on shared # column 'ID'
lf$Value <- duplicate_wide_to_long( wf, lf, x = Value, y = ID )
print( lf )
#>   ID Value
#> 1  1     4
#> 2  1     4
#> 3  1     4
#> 4  2     5
#> 5  2     5
#> 6  2     5
#> 7  3     6
#> 8  3     6
#> 9  3     6