Skip to content

Instantly share code, notes, and snippets.

@damianooldoni
Last active December 6, 2018 13:18
Show Gist options
  • Save damianooldoni/cffa32e60b5c9fb9d1cc633511d5ca99 to your computer and use it in GitHub Desktop.
Save damianooldoni/cffa32e60b5c9fb9d1cc633511d5ca99 to your computer and use it in GitHub Desktop.
New way of working row-wise in R via purrr package
#' Change value of a column (\code{remarks}) for rows with some fields equal to some rows in a second df (called here \code{df_ref}).
#' Notice that all \code{pmap()} family of functions demands same number of columns as the input df.
#' It is strongly recommended to use the column names as inputs of the function of \code{pmap()}.
#' In this specific example we require that \code{col3} is of type \code{character} and we use \code{pmap_chr()} to return a character.
library(purrr)
library(dplyr)
df <-
df %>%
mutate(col3 = pmap_chr(df %>%
select(col1, col2, col3),
function(col1, col2, col3) {
ifelse(all(c(col1, col2) %in% df_ref),
paste(na.omit(col3), "Add this"),
na.omit(col3))
})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment