在 R 中使用 rvest 获取坐标的编码问题?
Encoding problem using rvest for obtaining coordinates in R?
我正在尝试从维基百科中的 table 获取小数坐标。下面的代码让我一直拥有一个纬度列和一个经度列,但是我在将 longitude
列从字符转换为数字的最后一步失败了。相反,latitude
列转换得很好。
问题似乎是 longitude
列中每个字符串末尾的 'hidden' 个字符(str_length
'counts' 比在列值)。
这是编码问题吗?如何将 longitude
列转换为数字?
这段代码似乎让我明白了大部分内容:
# load packages
library(xml2)
library(rvest)
library(dplyr)
library(stringr)
library(tidyr)
library(readr)
# get coordinates data
webpage_url <- "https://en.wikipedia.org/wiki/List_of_Premier_League_stadiums"
webpage <- xml2::read_html(webpage_url)
# put web data into dataframe
df1 <- rvest::html_table(webpage, fill = TRUE)[[1]]
df2 <- df1 %>%
# split different coordinate formats
mutate(temp_Coordinates = str_split(string = Coordinates, pattern = " / ")) %>%
# one coordinate format per row
unnest(cols = temp_Coordinates) %>%
group_by(Stadium) %>%
# keep only 3rd row per stadium, i.e. decimal format of coordinates
filter(row_number() == 3) %>%
ungroup() %>%
# seperate coordinate pairs into individual columns for latitude and longitude
separate(temp_Coordinates, c("latitude","longitude"), sep = " ") %>%
# remove semi-colon from end of latitude string
mutate(latitude = str_replace(latitude, ";", ""))
问题似乎出在最后一步,将 longitude
从字符转换为数字(而不是生成一列 NA):
df3 <- df2 %>%
# convert latitude from character to numeric
mutate(latitude = as.numeric(latitude)) %>%
# convert longitude from character to numeric
mutate(longitude = as.numeric(longitude))
从 longitude
returns 中手动分配一个复制粘贴的值这个错误(注意当字符串被粘贴到控制台时出现奇怪的问号字符);
x <- "-2.96083�"
错误:在第 2 行读取 MBCS 字符时出现 EOF
有人知道如何更改格式以便我可以转换为数字吗?
谢谢!
您可以从一组不同的节点中提取值并分配给数据框
library(rvest)
library(magrittr)
webpage_url <- "https://en.wikipedia.org/wiki/List_of_Premier_League_stadiums"
webpage <- read_html(webpage_url)
df1 <- webpage %>% html_node('table') %>% html_table(fill = T)
geos <- webpage %>% html_nodes('.geo') %>% html_text() %>% str_split_fixed(., ';',2)
df1$latitude <- geos[, 1] %>% as.double()
df1$longitude <- geos[, 2] %>% as.double()
print(head(df1,1))
我正在尝试从维基百科中的 table 获取小数坐标。下面的代码让我一直拥有一个纬度列和一个经度列,但是我在将 longitude
列从字符转换为数字的最后一步失败了。相反,latitude
列转换得很好。
问题似乎是 longitude
列中每个字符串末尾的 'hidden' 个字符(str_length
'counts' 比在列值)。
这是编码问题吗?如何将 longitude
列转换为数字?
这段代码似乎让我明白了大部分内容:
# load packages
library(xml2)
library(rvest)
library(dplyr)
library(stringr)
library(tidyr)
library(readr)
# get coordinates data
webpage_url <- "https://en.wikipedia.org/wiki/List_of_Premier_League_stadiums"
webpage <- xml2::read_html(webpage_url)
# put web data into dataframe
df1 <- rvest::html_table(webpage, fill = TRUE)[[1]]
df2 <- df1 %>%
# split different coordinate formats
mutate(temp_Coordinates = str_split(string = Coordinates, pattern = " / ")) %>%
# one coordinate format per row
unnest(cols = temp_Coordinates) %>%
group_by(Stadium) %>%
# keep only 3rd row per stadium, i.e. decimal format of coordinates
filter(row_number() == 3) %>%
ungroup() %>%
# seperate coordinate pairs into individual columns for latitude and longitude
separate(temp_Coordinates, c("latitude","longitude"), sep = " ") %>%
# remove semi-colon from end of latitude string
mutate(latitude = str_replace(latitude, ";", ""))
问题似乎出在最后一步,将 longitude
从字符转换为数字(而不是生成一列 NA):
df3 <- df2 %>%
# convert latitude from character to numeric
mutate(latitude = as.numeric(latitude)) %>%
# convert longitude from character to numeric
mutate(longitude = as.numeric(longitude))
从 longitude
returns 中手动分配一个复制粘贴的值这个错误(注意当字符串被粘贴到控制台时出现奇怪的问号字符);
x <- "-2.96083�" 错误:在第 2 行读取 MBCS 字符时出现 EOF
有人知道如何更改格式以便我可以转换为数字吗?
谢谢!
您可以从一组不同的节点中提取值并分配给数据框
library(rvest)
library(magrittr)
webpage_url <- "https://en.wikipedia.org/wiki/List_of_Premier_League_stadiums"
webpage <- read_html(webpage_url)
df1 <- webpage %>% html_node('table') %>% html_table(fill = T)
geos <- webpage %>% html_nodes('.geo') %>% html_text() %>% str_split_fixed(., ';',2)
df1$latitude <- geos[, 1] %>% as.double()
df1$longitude <- geos[, 2] %>% as.double()
print(head(df1,1))