提问人:doraemon 提问时间:9/19/2023 最后编辑:doraemon 更新时间:9/19/2023 访问量:67
是否可以将包含数字和字符的列转换为 R 箭头中的数字?
Is it possible to convert a column contain numeric and character into numeric in R arrow?
问:
例如 如果我运行以下代码:
library(arrow)
library(tidyverse)
mtcars$cyl[2:5] = 'a'
mtcars %>%
arrow_table() %>%
mutate(cyl = cast(cyl, numeric())) %>%
collect()
弹出一条错误消息:
Error in `compute.arrow_dplyr_query()`:
! Invalid: Failed to parse string: 'a' as a scalar of type double
Run `rlang::last_trace()` to see where the error occurred.
我希望结果是没有错误的,那些“a”将被转换为.但是,函数的性质似乎与 r 中的性质不同。NA
cast
as.numeric
答:
1赞
Nir Graham
9/19/2023
#1
似乎有效
library(arrow)
library(tidyverse)
library(stringr)
mtcars$cyl[2:5] = 'a'
mtcars %>%
arrow_table() %>%
mutate(
cyl = cast(if_else(str_detect(cyl, "^[0-9]+$"),
cyl,NA_character_), numeric())) %>%
collect()
评论
as.numeric
cast
as.numeric
NA