You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A proposed function to adapt a typical plyr::mapvalues use case to dplyr
The advent of case_match has brought many of the recoding functionality of plyr::mapvalues into dplyr.
However, a use case remains where there is a long vector of from values, and a long vector of to values, and plyr::mapvalues lets you concisely express this in a way that case_when() and case_match() don't replicate.
My (primitive! risible!) attempt at a working function (I understand it would have to be adapted to match tidyverse style if deemed a candidate for inclusion
library(Rcpp)
# Define the C++ function for character vectors
cppFunction('CharacterVector case_map_cpp(CharacterVector x, CharacterVector from, CharacterVector to) { int n = x.size(); CharacterVector result(n); for (int i = 0; i < n; ++i) { std::string value = Rcpp::as<std::string>(x[i]); for (int j = 0; j < from.size(); ++j) { if (value == Rcpp::as<std::string>(from[j])) { result[i] = to[j]; break; } else { result[i] = x[i]; } } } return result;}')
# R wrapper function to handle any types by converting them to character vectorscase_map<-function(x, from, to) {
# Convert all input to character vectors to standardize processingx_char<- as.character(x)
from_char<- as.character(from)
to_char<- as.character(to)
# Call the C++ functionmapped_values<- case_map_cpp(x_char, from_char, to_char)
# Check if the original input was numeric and if all 'to' values can be converted to numeric# Use 'suppressWarnings' to avoid warnings during conversion checksshould_convert_back<- is.numeric(x) &&!any(is.na(suppressWarnings(as.numeric(to_char))))
# Convert back to numeric if appropriate, otherwise return as characterif (should_convert_back) {
return(as.numeric(mapped_values))
} else {
return(mapped_values)
}
}
expr min lq mean median uq max neval
688.2 710.3 729.576 714.55 720.6 1181.3 100
expr min lq mean median uq max neval
545.1 694.5 823.852 759.2 785.4 8377 100
expr min lq mean median uq max neval
1.1532 1.3287 1.517161 1.347 1.3875 9.4159 100
The text was updated successfully, but these errors were encountered:
Eventually we will add vec_case_match() to vctrs, which is a more programmatic version you can do this with. It is currently prototyped in dplyr and powers case_match(). It would look like this to get the same results as above:
A proposed function to adapt a typical
plyr::mapvalues
use case to dplyrThe advent of
case_match
has brought many of the recoding functionality ofplyr::mapvalues
intodplyr
.However, a use case remains where there is a long vector of
from
values, and a long vector ofto
values, andplyr::mapvalues
lets you concisely express this in a way thatcase_when()
andcase_match()
don't replicate.My (primitive! risible!) attempt at a working function (I understand it would have to be adapted to match
tidyverse
style if deemed a candidate for inclusionIt seems reasonably performant
should return
The text was updated successfully, but these errors were encountered: