小编ele*_*ron的帖子

在html_table(rvest)中指定列类

我使用rvest的html_table从下面的网站上读取两列索引表.两列都包含我想要保留的前导零的实例.因此,我希望列是类字符.我使用以下代码:

library(rvest)
library(data.table)

df <- list() 
for (j in 1:25) {
     url <- paste('http://unstats.un.org/unsd/cr/registry/regso.asp?Ci=70&Lg=1&Co=&T=0&p=',
           j, '&prn=yes', sep='') 
     webpage <- read_html(url) 
     table <- html_nodes(webpage, 'table') 
     df[[j]] <- html_table(table, header=TRUE)[[1]] 
     df[[j]] <- df[[j]][,c(1:2) ] 
}
ISIC4.NACE2 <- rbindlist(df) 
Run Code Online (Sandbox Code Playgroud)

但是str(df [[1]])返回

'data.frame':   40 obs. of  2 variables:
$ ISIC Rev.4: chr  "01" "011" "0111" "0112" ...
$ NACE Rev.2: num  1 1.1 1.11 1.12 1.13 1.14 1.15 1.16 1.19 1.2 ...
Run Code Online (Sandbox Code Playgroud)

似乎html_table函数将第一列解释为字符,将第二列解释为数字,从而截断后者中的前导零.有没有办法使用html_table指定列类?

html r html-table rvest

7
推荐指数
1
解决办法
1939
查看次数

标签 统计

html ×1

html-table ×1

r ×1

rvest ×1