Learn R Programming

easyr (version 0.5-11)

jrepl: Join and Replace Values.

Description

Replace a columns values with matches in a different dataset. Author: Bryce Chamberlain.

Usage

jrepl(
  x,
  y,
  by,
  replace.cols,
  na.only = FALSE,
  only.rows = NULL,
  verbose = FALSE
)

Value

x with new values.

Arguments

x

Main dataset which will have new values. This data set will be returned with new values.

y

Supporting dataset which has the id and new values.

by

Vector of join column names. A character vector if the names match. A named character vector if they don't.

replace.cols

Vector of replacement column names, similar format as by.

na.only

Only replace values that are NA.

only.rows

Select rows to be affected. Default checks all rows.

verbose

Print via cat information about the replacement.

Examples

Run this code

df1 = utils::head( sleep )
group.reassign = data.frame( 
  id.num = factor( c( 1, 3, 4 ) ), 
group.replace = factor( c( 99, 99, 99 ) ) 
)

jrepl( 
  x = df1, 
  y = group.reassign, 
  by = c( 'ID' = 'id.num' ), 
  replace.cols = c( 'group' = 'group.replace' ) 
)

# doesn't affect since there are no NAs in group.
jrepl( 
  x = df1,
  y = group.reassign, 
  by = c( 'ID' = 'id.num' ), 
  replace.cols = c( 'group' = 'group.replace' ), 
  na.only = TRUE  
) 

Run the code above in your browser using DataLab