2017-12-18 131 views
0

有关此数据提取的问题我做了。我想创建一个带有数据的条形图,但不幸的是,我无法将提取的字符转换为R中的数字。如果我在文本编辑器中编辑该文件,根本没有porblem,但是我想要做的全部在R.过程这是代码:R:将字符转换为R data.frame中的数字

install.packages("rvest") 
    library(rvest) 

    url <- "https://en.wikipedia.org/wiki/Corporate_tax" 

    corporatetax <- url %>% 
    read_html() %>% 
    html_nodes(xpath='//*[@id="mw-content-text"]/div/table[5]') %>% 
    html_table() 

    str(corporatetax) 

corporatetax结果是,有3个变量所有这些字符的data.frame。我还没有解决的问题是,我应该如何将第二和第三列转换为数字来创建条形图?我尝试过使用sapply()和dplyr(),但没有找到正确的方法来做到这一点。

谢谢!

+2

您是否尝试过as.numeric并将其应用到每个要转换的列?例如df $ column_1 < - as.numeric(df $ column_1) –

+0

您可能需要删除非数字字符,比如“%”,我通常使用'gsub()'作为此 – Nate

+0

和现在的“corporatetax”是一个列表,而不是一个data.frame。用'corporatetax [[1]]'提取data.frame – Nate

回答

0

您可以尝试清理桌子这样

library(rvest) 
library(stringr) 
library(dplyr) 

url <- "https://en.wikipedia.org/wiki/Corporate_tax" 

corporatetax <- url %>% 
    read_html() %>% 
    # your xpath defines the single table, so you can use html_node() instead of html_nodes() 
    html_node(xpath='//*[@id="mw-content-text"]/div/table[5]') %>% 
    html_table() %>% as_tibble() %>% 
    setNames(c("country", "corporate_tax", "combined_tax")) 

corporatetax %>% 
    mutate(corporate_tax=as.numeric(str_replace(corporate_tax, "%", ""))/100, 
     combined_tax=as.numeric(str_replace(combined_tax, "%", ""))/100 
     )