The issue would be that some of the values are duplicates, so, it would return the error. An option is rowwise
library(usmap)
library(dplyr)
act_19 %>%
rowwise %>%
mutate(state_abb = fips_function(GESTFIPS)) %>%
ungroup
-output
# A tibble: 3 x 2
# GESTFIPS state_abb
# <dbl> <chr>
#1 40 OK
#2 51 VA
#3 40 OK
Or another option is to run this on the distinct
values of 'GESTFIPS' and then do a join
act_19 %>%
distinct(GESTFIPS) %>%
mutate(state_abb = fips_function(GESTFIPS)) %>%
right_join(act_19)
-output
# A tibble: 3 x 2
# GESTFIPS state_abb
# <dbl> <chr>
#1 40 OK
#2 40 OK
#3 51 VA
The error can be reproduced with the duplicate values
act_19 %>%
mutate(state_abb = fips_function(GESTFIPS))
Error: Problem with mutate()
input state_abb
.
? Input state_abb
can't be recycled to size 3.
? Input state_abb
is fips_function(GESTFIPS)
.
? Input state_abb
must be size 3 or 1, not 2.
Run rlang::last_error()
to see where the error occurred.
This issue arises directly from subsetting
usmap:::get_fips_info
function (fips)
{
if (all(nchar(fips) == 2)) {
df <- utils::read.csv(system.file("extdata", "state_fips.csv",
package = "usmap"), colClasses = rep("character",
3), stringsAsFactors = FALSE)
result <- df[df$fips %in% fips, ] # -> would subset only for unique fips
...
data
act_19 <- tibble(GESTFIPS = c(40, 51, 40))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…