是否可以将包含数字和字符的列转换为R箭头中的数字?

问题描述 投票:0回答:1

例如, 如果我运行以下代码:

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”将被转换为

NA
。然而,
cast
函数的性质似乎与r中的
as.numeric
不一样。

r character numeric apache-arrow
1个回答
0
投票

试试这个:

mtcars %>%
  mutate(cyl = as.numeric(cyl))

                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0  NA 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8  NA 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4  NA 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7  NA 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
© www.soinside.com 2019 - 2024. All rights reserved.