Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bind_rows(), column can't be converted from integer to character error

Tags:

r

dplyr

I have seen this answer: Error in bind_rows_(x, .id) : Column can't be converted from factor to numeric but I can't mutate_all() a list.

library(rvest)
library(dplyr)
library(tidyr)

fips <- read_html("https://www.census.gov/geo/reference/ansi_statetables.html") %>% 
  html_nodes("table") %>% 
  html_table() %>% 
  bind_rows(.[[1]][1:3] %>% 
          transmute(name = `Name`,
                    fips = as.character(`FIPS State Numeric Code`),
                    abb = `Official USPS Code`),
        filter(.[[2]][1:3], !grepl("Status",c(`Area Name`))) %>% 
          transmute(name = `Area Name`,
                    fips = as.character(`FIPS State Numeric Code`),
                    abb = `Official USPS Code`))

Error in bind_rows_(list_or_dots(...), id = NULL) : 
  Column `FIPS State Numeric Code` can't be converted from integer to character

This code however works just fine:

fips <- read_html("https://www.census.gov/geo/reference/ansi_statetables.html")

dat3 <- fips %>%
  html_nodes("table") %>% 
  html_table()

rbind(dat3[[1]][1:3] %>% 
        transmute(name = `Name`,
                  fips = `FIPS State Numeric Code`,
                  abb = `Official USPS Code`),
filter(dat3[[2]][1:3], !grepl("Status",c(`Area Name`))) %>% 
  transmute(name = `Area Name`,
            fips = `FIPS State Numeric Code`,
            abb = `Official USPS Code`))
like image 463
SCDCE Avatar asked Mar 06 '23 20:03

SCDCE


1 Answers

As pointed out by @akrun in the comments, bind_rows is type sensitive. Therefore, I would first use lapply within dplyr to mutate_if over the list and then bind_rows of the character data frames, setNames to be able to call the variable in filtering by Area_Name in the final step:

fips <- read_html("https://www.census.gov/geo/reference/ansi_statetables.html") %>% 
  html_nodes("table") %>% 
  html_table() %>% 
  lapply(., mutate_if, is.integer, as.character) %>%
  bind_rows() %>%
  setNames(gsub(" ", "_", names(.))) %>%
  filter(!grepl("Status", Area_Name)) %>%
  mutate(Name = ifelse(is.na(Name), Area_Name, Name)) %>%
  select(Name, FIPS_State_Numeric_Code, Official_USPS_Code)
like image 80
Nic Avatar answered May 12 '23 17:05

Nic