将HTML表转换为R数据帧

时间:2015-09-04 14:47:52

标签: html r xml-parsing dataframe

<TABLE  cellspacing=1 cellpadding=7 rules=all frame=Box border=1>
<thead>
<TR>
 <TD ROWSPAN=2 ALIGN=CENTER VALIGN=CENTER>&nbsp;</TD>
 <TD COLSPAN=6 ALIGN=CENTER>1a. My peers make a positive impact my work environment.</TD>
 <TD ALIGN=CENTER>Number</TD>
</TR>
<TR>
 <TD ALIGN=CENTER>Strongly agree  <br>         </TD>
 <TD ALIGN=CENTER>Generally agree <br>         </TD>
 <TD ALIGN=CENTER>Neither agree nor<br>disagree</TD>
 <TD ALIGN=CENTER>Generally disagree<br>       </TD>
 <TD ALIGN=CENTER>Strongly disagree<br>        </TD>
 <TD ALIGN=CENTER>No basis to judge<br>        </TD>
 <TD ALIGN=CENTER>of Cases</TD>
</TR>
</thead>
<tbody>
<TR>
 <TD ALIGN=LEFT VALIGN=TOP>  Company-Wide                                     </TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>        44.1</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>        44.9</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>         6.6</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>         2.6</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>         1.6</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>         0.1</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>   2,014</TD>
</TR>
<TR>
 <TD ALIGN=LEFT VALIGN=TOP> Region 1                                 </TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>        45.6</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>        45.2</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>         5.7</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>         2.1</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>         1.4</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>         0.1</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>   1,699</TD>
</TR>
<TR>
 <TD ALIGN=LEFT VALIGN=TOP>Division 1            </TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>        52.9</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>        39.7</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>         4.1</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>         2.5</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>         0.8</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>0</TD>
 <TD ALIGN=RIGHT VALIGN=BOTTOM>     121</TD>
</TR>
</tbody>
</TABLE>
<hr><A NAME="IDX1">&nbsp;</A>

我有一个HTML文件,其中包含上面几种类型的表。我想将它们转换为数据框,其中每个调查问题(当前在表格标题中)将出现在一列中。对每个问题的响应百分比将保留在列中,响应级别也是如此。并非所有问题都有相同数量的答案(即一些问题是五分制,另一些则是九分制)。我尝试了readHTMLTable,然后对该结果执行了.call rbind,但由于列数不相同而无法获得感兴趣的数据帧。我欢迎任何关于如何进行的建议。谢谢!

编辑:

library(xml)
library(dplyr)
questions<-readHTMLTable(files[8], trim=T, as.data.frame=T, header=T)
data<-bind_rows(questions)

我想要的数据框中的结果,但由于某些问题的响应级别高于其他问题,因此“案例数”数据不会始终显示在一列中。有没有办法让我在合并之前命名每个表的最后一列?

1 个答案:

答案 0 :(得分:2)

您可以使用rvest包。但是,可能需要注意带有空格的列名。我使用选项fill=TRUE作为快速修复,但也许这可以更好的方式完成。

library(rvest)
my_df <- as.data.frame(read_html(text) %>% html_table(fill=TRUE))
> my_df
#              X1                                                       X2                        X3                 X4                X5                X6       X7     X8
#1                1a. My peers make a positive impact my work environment.                      <NA>               <NA>              <NA>              <NA>     <NA> Number
#2 Strongly agree                                          Generally agree Neither agree nordisagree Generally disagree Strongly disagree No basis to judge of Cases   <NA>
#3   Company-Wide                                                     44.1                      44.9                6.6               2.6               1.6      0.1  2,014
#4       Region 1                                                     45.6                      45.2                5.7               2.1               1.4      0.1  1,699
#5     Division 1                                                     52.9                      39.7                4.1               2.5               0.8        0    121

关于数据,我使用单引号将来自OP的html代码复制粘贴并使用text将其分配给变量text <- '<TABLE cellspacing=1 cellpadding=7 rules=all frame=...'

之后可以通过一种相当简单的方式纠正格式的一些细节:

my_df[2,] <- c("",my_df[2,][-length(my_df)])
#> my_df
#            X1                                                       X2              X3                        X4                 X5                X6                X7       X8
#1              1a. My peers make a positive impact my work environment.            <NA>                      <NA>               <NA>              <NA>              <NA>   Number
#2                                                        Strongly agree Generally agree Neither agree nordisagree Generally disagree Strongly disagree No basis to judge of Cases
#3 Company-Wide                                                     44.1            44.9                       6.6                2.6               1.6               0.1    2,014
#4     Region 1                                                     45.6            45.2                       5.7                2.1               1.4               0.1    1,699
#5   Division 1                                                     52.9            39.7                       4.1                2.5               0.8                 0      121

基本上,在这种情况下,第二行的条目应该向右移动一个单元格。

数据

text <- '<TABLE  cellspacing=1 cellpadding=7 rules=all frame=Box border=1>\n  <thead>\n  <TR>\n  <TD ROWSPAN=2 ALIGN=CENTER VALIGN=CENTER>&nbsp;</TD>\n    <TD COLSPAN=6 ALIGN=CENTER>1a. My peers make a positive impact my work environment.</TD>\n      <TD ALIGN=CENTER>Number</TD>\n        </TR>\n        <TR>\n        <TD ALIGN=CENTER>Strongly agree  <br>         </TD>\n          <TD ALIGN=CENTER>Generally agree <br>         </TD>\n            <TD ALIGN=CENTER>Neither agree nor<br>disagree</TD>\n              <TD ALIGN=CENTER>Generally disagree<br>       </TD>\n                <TD ALIGN=CENTER>Strongly disagree<br>        </TD>\n                  <TD ALIGN=CENTER>No basis to judge<br>        </TD>\n                    <TD ALIGN=CENTER>of Cases</TD>\n                      </TR>\n                      </thead>\n                      <tbody>\n                      <TR>\n                      <TD ALIGN=LEFT VALIGN=TOP>  Company-Wide                                     </TD>\n                        <TD ALIGN=RIGHT VALIGN=BOTTOM>        44.1</TD>\n                          <TD ALIGN=RIGHT VALIGN=BOTTOM>        44.9</TD>\n                            <TD ALIGN=RIGHT VALIGN=BOTTOM>         6.6</TD>\n                              <TD ALIGN=RIGHT VALIGN=BOTTOM>         2.6</TD>\n                                <TD ALIGN=RIGHT VALIGN=BOTTOM>         1.6</TD>\n                                  <TD ALIGN=RIGHT VALIGN=BOTTOM>         0.1</TD>\n                                    <TD ALIGN=RIGHT VALIGN=BOTTOM>   2,014</TD>\n                                      </TR>\n                                      <TR>\n                                      <TD ALIGN=LEFT VALIGN=TOP> Region 1                                 </TD>\n                                        <TD ALIGN=RIGHT VALIGN=BOTTOM>        45.6</TD>\n                                          <TD ALIGN=RIGHT VALIGN=BOTTOM>        45.2</TD>\n                                            <TD ALIGN=RIGHT VALIGN=BOTTOM>         5.7</TD>\n                                              <TD ALIGN=RIGHT VALIGN=BOTTOM>         2.1</TD>\n                                                <TD ALIGN=RIGHT VALIGN=BOTTOM>         1.4</TD>\n                                                  <TD ALIGN=RIGHT VALIGN=BOTTOM>         0.1</TD>\n                                                    <TD ALIGN=RIGHT VALIGN=BOTTOM>   1,699</TD>\n                                                      </TR>\n                                                      <TR>\n                                                      <TD ALIGN=LEFT VALIGN=TOP>Division 1            </TD>\n                                                        <TD ALIGN=RIGHT VALIGN=BOTTOM>        52.9</TD>\n                                                          <TD ALIGN=RIGHT VALIGN=BOTTOM>        39.7</TD>\n                                                            <TD ALIGN=RIGHT VALIGN=BOTTOM>         4.1</TD>\n                                                              <TD ALIGN=RIGHT VALIGN=BOTTOM>         2.5</TD>\n                                                                <TD ALIGN=RIGHT VALIGN=BOTTOM>         0.8</TD>\n                                                                  <TD ALIGN=RIGHT VALIGN=BOTTOM>0</TD>\n                                                                    <TD ALIGN=RIGHT VALIGN=BOTTOM>     121</TD>\n                                                                      </TR>\n                                                                      </tbody>\n                                                                      </TABLE>\n                                                                      <hr><A NAME=\"IDX1\">&nbsp;</A>'
#> class(text)
#[1] "character"