我看到了以下答案:Error in bind_rows_(x, .id) : Column can't be converted from factor to numeric,但我无法mutate_all()
列表。
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
但是此代码可以正常工作:
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`))
答案 0 :(得分:2)
正如@akrun在评论中指出的那样,bind_rows是类型敏感的。因此,我将首先使用dplyr中的lapply
来遍历列表中的mutate_if
,然后使用bind_rows
个字符数据帧setNames
来调用{{ 1}}在最后一步按Area_Name:
filter