English · PDF 156
parse_integer(c("1", "231", ".", "456"), na = ".") #> [1] 1 231 NA 456
如果解析失败,你会得到一个警告:
x <- parse_integer(c("123", "345", "abc", "123.45")) #> Warning: 2 parsing failures. #> row col expected actual #> 3 -- an integer abc #> 4 -- no trailing characters .45
并且失败的部分会在输出中缺失:
x #> [1] 123 345 NA NA #> attr(,"problems") #> # A tibble: 2 × 4 #> row col expected actual #>
如果解析失败很多,你需要使用 problems() 来获取完整的集合。它返回一个 tibble,然后你可以用 dplyr 对其进行操作:
problems(x) #> # A tibble: 2 × 4 #> row col expected actual #>
使用解析器主要是要了解有哪些可用的解析器,以及它们如何处理不同类型的输入。有八个特别重要的解析器: