How to Use mapvalues() Function in r


Often you may want to replace multiple patterns in a particular string in R with multiple new patterns.

One of the easiest ways to do so is by using the mapvalues() function from the plyr package in R, which can be used to perform this exact task.

The mapvalues() function uses the following syntax:

mapvalues(x, from, to, warn_missing = TRUE)

where:

  • x: Name of an input character vector
  • from: A vector of items to replace
  • to: A vector of replacement values
  • warn_missing: Whether to print a message if any old values are not present

The following example shows how to use the mapvalues() function in practice to replace multiple patterns in strings in R.

Note: Before using the mapvalues() function, you may need to first install the plyr package. You can use the following syntax to do so:

install.packages('plyr')

Once the stringr package has been installed, you can proceed to use the mapvalues() function.

Example: How to Use the mapvalues() Function in R

Suppose that we create the following vector that contains various strings:

#create vector of strings
my_strings <- c('Mavs', 'Warriors', 'Thunder', 'Raptors', 'Cavs', 'Heat')

#view vector
my_strings

[1] "Mavs"     "Warriors" "Thunder"  "Raptors"  "Cavs"     "Heat"

Suppose that we would like to make the following replacements in this vector:

  • Replace “Mavs” with “Mavericks”
  • Replace “Cavs” with “Cavaliers”

We can use the mapvalues() function with the following syntax to do so:

library(plyr)

#make multiple string replacements
mapvalues(my_strings, c('Mavs', 'Cavs'), c('Mavericks', 'Cavaliers'))

[1] "Mavericks" "Warriors"  "Thunder"   "Raptors"   "Cavaliers" "Heat"

Notice that “Mavs” has been replaced with “Mavericks” and “Cavs” has been replaced with “Cavaliers” in the vector.

It’s worth noting that the mapvalues() function will replace all occurrences of a particular pattern with a new replacement pattern.

For example, suppose we had a vector that contained two occurrences of “Mavs” and we wanted to replace each occurrence.

We could use the mapvalues() function to do so:

library(plyr)

#create vector of strings
my_strings <- c('Mavs', 'Mavs', 'Thunder', 'Raptors', 'Cavs', 'Heat')

#make multiple string replacements
mapvalues(my_strings, c('Mavs', 'Cavs'), c('Mavericks', 'Cavaliers'))

[1] "Mavericks" "Mavericks"  "Thunder"   "Raptors"   "Cavaliers" "Heat"

Notice that both occurrences of “Mavs” in the vector have been replaced with “Mavericks” as the new replacement string.

Also note that if you search for a pattern to replace in the vector and the pattern does not exist then a warning message will be shown.

For example, suppose we attempted to replace “Lavs” instead:

library(plyr)

#create vector of strings
my_strings <- c('Mavs', 'Warriors', 'Thunder', 'Raptors', 'Cavs', 'Heat')

#attempt to make multiple string replacements
mapvalues(my_strings, c('Lavs', 'Cavs'), c('Mavericks', 'Cavaliers'))

The following `from` values were not present in `x`: Lavs
[1] "Mavs"      "Warriors"  "Thunder"   "Raptors"   "Cavaliers" "Heat"   

Since the pattern “Lavs” does not exist in any of the strings in the vector, a warning message is shown to let us know that this pattern was not found.

The pattern “Cavs” was still replaced with the new pattern of “Cavaliers” though.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use str_remove in R
How to Use str_match in R
How to Use str_pad in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *