Html 如何用R中的NA替换空单元格?

Html 如何用R中的NA替换空单元格?,html,r,xml,na,Html,R,Xml,Na,我是R的新手,已经尝试了很多例子,但是我无法将我所有的空单元格都变成NA library(XML) theurl <- "http://www.pro-football-reference.com/teams/sfo/1989.htm" table <- readHTMLTable(theurl) table 库(XML) URL从readHTMLTable得到的结果是一个包含两个表的列表,因此您需要处理每个列表元素,这可以使用lapply table <- lapply(t

我是R的新手,已经尝试了很多例子,但是我无法将我所有的空单元格都变成NA

library(XML)
theurl <- "http://www.pro-football-reference.com/teams/sfo/1989.htm"
table <- readHTMLTable(theurl)
table
库(XML)

URL从
readHTMLTable
得到的结果是一个包含两个表的列表,因此您需要处理每个列表元素,这可以使用
lapply

table <- lapply(table, function(x){
     x[x == ""] <- NA
     return(x)
})


table$team_stats
Player  PF  Yds  Ply  Y/P TO FL 1stD  Cmp Att  Yds TD Int NY/A 1stD Att  Yds TD Y/A 1stD  Pen  Yds 1stPy
1      Team Stats 442 6268 1021  6.1 25 14  350  339 483 4302 35  11  8.1  209 493 1966 14 4.0  124  109  922    17
2      Opp. Stats 253 4618  979  4.7 37 16  283  316 564 3235 15  21  5.3  178 372 1383  9 3.7   76   75  581    29
3 Lg Rank Offense   1    1 <NA> <NA>  2 10    1 <NA>  20    2  1   1    1 <NA>  13   10 12  13 <NA> <NA> <NA>  <NA>
4 Lg Rank Defense   3    4 <NA> <NA> 11  9    9 <NA>  25   11  3   9    5 <NA>   1    3  3   8 <NA> <NA> <NA>  <NA>

table虽然实际数据大多是数字,但您有一个数据列表。使用
type转换为适当的类型。convert
将自动为您插入适当的
NA
s:

df_list <- lapply(table, function(x){
    x[] <- lapply(x, function(y){type.convert(as.character(y), as.is = TRUE)}); 
    x
})

df_list[[1]][, 1:18]
##            Player  PF  Yds  Ply Y/P TO FL 1stD Cmp Att Yds.1 TD Int NY/A 1stD.1 Att.1 Yds.2 TD.1
## 1      Team Stats 442 6268 1021 6.1 25 14  350 339 483  4302 35  11  8.1    209   493  1966   14
## 2      Opp. Stats 253 4618  979 4.7 37 16  283 316 564  3235 15  21  5.3    178   372  1383    9
## 3 Lg Rank Offense   1    1   NA  NA  2 10    1  NA  20     2  1   1  1.0     NA    13    10   12
## 4 Lg Rank Defense   3    4   NA  NA 11  9    9  NA  25    11  3   9  5.0     NA     1     3    3
df\u列表%type\u convert())
df_列表[[1]]
###A tibble:4×23
##球员pf yds ply YU p到fl x1std cmp att yds YU 2 td int ny_a
##                          
##1球队统计数据442 6268 1021 6.1 25 14 350 339 483 4302 35 11 8.1
##2 Opp.Stats 253 4618 979 4.7 37 16 283 316 564 3235 15 21 5.3
##3 Lg等级进攻1 NA 2 10 1 NA 20 2 1 1.0
##4 Lg等级防御3 4 NA 11 9 NA 25 11 3 9 5.0
## # ... 还有9个变量:x1std_2,att_2,yds_3,td_2,y_a,
###x1std_3,pen,yds_4,x1stpy

插上电源,它就像一个魔咒,非常受欢迎。是时候了解一下拉普利的工作了
lappy
*apply
函数组中的一个,它在一组数据上“应用”一个函数-通常用在R中代替“for”循环。在这种情况下,
lappy
列表上应用函数(因此
l
):)
library(tidyverse)    # for purrr functions and readr::type_convert
library(janitor)      # for clean_names

df_list <- map(table, ~.x %>% clean_names() %>% dmap(as.character) %>% type_convert())

df_list[[1]]
## # A tibble: 4 × 23
##            player    pf   yds   ply   y_p    to    fl x1std   cmp   att yds_2    td   int  ny_a
##             <chr> <int> <int> <int> <dbl> <int> <int> <int> <int> <int> <int> <int> <int> <dbl>
## 1      Team Stats   442  6268  1021   6.1    25    14   350   339   483  4302    35    11   8.1
## 2      Opp. Stats   253  4618   979   4.7    37    16   283   316   564  3235    15    21   5.3
## 3 Lg Rank Offense     1     1    NA    NA     2    10     1    NA    20     2     1     1   1.0
## 4 Lg Rank Defense     3     4    NA    NA    11     9     9    NA    25    11     3     9   5.0
## # ... with 9 more variables: x1std_2 <int>, att_2 <int>, yds_3 <int>, td_2 <int>, y_a <dbl>,
## #   x1std_3 <int>, pen <int>, yds_4 <int>, x1stpy <int>