Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用rvest获取R中坐标的编码问题?_R_Encoding_Character Encoding_Tidyverse_Rvest - Fatal编程技术网

使用rvest获取R中坐标的编码问题?

使用rvest获取R中坐标的编码问题?,r,encoding,character-encoding,tidyverse,rvest,R,Encoding,Character Encoding,Tidyverse,Rvest,我试图从维基百科的一个表中获取十进制坐标。下面的代码让我一直拥有一列表示纬度和一列表示经度,但在将经度列从字符转换为数字的最后一步,我失败了。相反,纬度列可以很好地进行转换 问题似乎是经度列中每个字符串末尾都有一个“隐藏”字符(str_length比列值中可见的字符多“计数”一个字符) 这是编码问题吗?如何将经度列转换为数字 这段代码似乎在很大程度上帮助了我: # load packages library(xml2) library(rvest) library(dplyr) library(

我试图从维基百科的一个表中获取十进制坐标。下面的代码让我一直拥有一列表示纬度和一列表示经度,但在将
经度
列从字符转换为数字的最后一步,我失败了。相反,
纬度
列可以很好地进行转换

问题似乎是
经度
列中每个字符串末尾都有一个“隐藏”字符(
str_length
比列值中可见的字符多“计数”一个字符)

这是编码问题吗?如何将
经度
列转换为数字

这段代码似乎在很大程度上帮助了我:

# 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, ";", ""))
经度
手动指定复制和粘贴值将返回此错误(请注意将字符串粘贴到控制台时出现的奇怪问号字符)


x您可以从一组不同的节点中提取值并分配给数据帧

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))
库(rvest)
图书馆(magrittr)
网页url%html\U text()%%>%str\u拆分\u已修复(,“;”,2)
df1$latitude%as.double()
df1$longitude%as.double()
打印(打印头(df1,1))

您是否尝试过
编码(经度)='UTF-8'
?我尝试过,但结果相同
Encoding(df2$longitude)
还表明该列已经在UTF-8中了。谢谢。请问输出行格式的示例是什么?对不起,我不确定我是否理解这个问题。我认为,我的问题是特定列中字符的格式问题。所需的输出是能够将表单字符转换为数字。那么,对于最终df中的一行,这种转换是什么样子的呢?最终的lat和lon值。
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))