Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

dplyr - R how to mutate a new column that has the same vector size

I am trying to create a new column that converts FIPS code to the state abbreviation using library(usmap), the problem is that the new column after using mutate does not match the matrix size. The new column only has 51 rows after using fips_info, but not 23570 rows of the original matrix.

Appreciate any help, thanks!

#defined function to get state abb
fips_function <- function(fips_code){
  return (fips_info(fips_code)$abbr)
}

atus_19_selected <- act_19 %>%
  mutate(state_abb = fips_function(GESTFIPS))

Error: Problem with `mutate()` input `state_abb`.
x Input `state_abb` can't be recycled to size 23570.
? Input `state_abb` is `fips_function(GESTFIPS)`.
? Input `state_abb` must be size 23570 or 1, not 51.


atus_19_selected
# A tibble: 23,570 x 8
   GESTFIPS GTCO  TUCASEID t150701 t150799 t150801 t150899 t159999
      <dbl> <chr>    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1       40 000    2.02e13      60       0       0       0       0
 2       51 153    2.02e13      40       0       0       0     260


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...