r - 解释 R 中的错​​误消息,将一列分成两列并创建一个新的数据框

标签 r error-handling split strsplit

我已经将两个表刮到 R 中,修剪它们,现在我试图通过将原始列中的一列分成两列来创建新表。为此,我编写了以下代码:

page.201702050atl = read_html("http://www.pro-football-reference.com/boxscores/201702050atl.htm")
comments.201702050atl = page.201702050atl %>% html_nodes(xpath = "//comment()")
home.drives.201702050atl = comments.201702050atl[43] %>% html_text() %>% read_html() %>% html_node("#home_drives") %>% html_table()
home.drives.201702050atl.a = home.drives.201702050atl[ , 2:8]
LOS.home.201702050atl = t(data.frame(strsplit(as.character(home.drives.201702050atl.a$LOS), " ", 2)))
LOS.vis.201702050atl = t(data.frame(strsplit(as.character(home.drives.201702050atl.a$LOS), " ", 2)))

我已经在无数其他表上无数次地运行了这段代码;但是,在这两个(几乎相同的)表上,我似乎遇到了一个我不明白的问题:
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 2, 0

有人可以帮我把这个错误信息翻译成英文吗?在这种特殊情况下有什么问题?最后,我该如何解决?

编辑:

home.drives.201702050atl 的第 5 行缺少一个单元格。最初我认为这可能是问题所在;然而,这就是为什么我包括第二张表。在第二个表中,没有缺失单元格;然而,我遇到了同样的错误。虽然它与第一个表的相似,但我没有在我的原始帖子中包含第二个表的全部编码。请在下面找到它:
> page.201702050atl = read_html("http://www.pro-football-reference.com/boxscores/201702050atl.htm")
> comments.201702050atl = page.201702050atl %>% html_nodes(xpath = "//comment()")
> vis.drives.201702050atl = comments.201702050atl[44] %>% html_text() %>% read_html() %>% html_node("#vis_drives") %>% html_table()
> vis.drives.201702050atl.a = vis.drives.201702050atl[ , 2:8]
> LOS.vis.201702050atl = t(data.frame(strsplit(as.character(home.drives.201702050atl.a$LOS), " ", 2)))
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 2, 0

最佳答案

错误来自代码的第五行。

home.drives.201702050atl.a %>% 
    `$`(LOS) %>%
    as.character() %>%
    strsplit(., " ", 2) %>%
    purrr::map_int(length)

[1] 2 2 2 2 0 2 2 2 2 2 2

第五个元素的长度为0。错误是创建具有不同行数的数据框的结果。摆脱第五个元素可能会解决问题。

关于r - 解释 R 中的错​​误消息,将一列分成两列并创建一个新的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42712952/

相关文章:

exception-handling - 在业务层上生成的在UI上显示异常消息的建议方式是什么?

go - os.PathError 未实现错误

python - 附加一个 numpy 数组,但以二维方式组织附加信息

sql - 插入到表中并在 SQL 中拆分字符串

r - 根据匹配值覆盖数据框中的特定值

r - 如何根据列值删除行

r - Glmertree 选项类似于 partykit::mob

R 根据事件更新值

javascript - 在这种情况下,抛出自定义错误而不是在调用者中处理响应(例如 null)是否好?

math - 分割贝塞尔曲线