首页 > 解决方案 > dplyr::mutate 是否适用于记录样式的列?

问题描述

我最近一直在测试 vctrs 包,尤其是最近他们所谓的“记录样式”对象,我想知道是否有任何方法可以让它们与 dplyr::mutate 配合得很好。目前,当 dplyr::mutate 在我尝试使用对象时给我一个关于对象长度的错误。

我不知道合适的内置类型,所以作为一个代表,我将使用这个小插图中描述的理性类。

library("vctrs")
library("dplyr")
new_rational <- function(n = integer(), d = integer()) {
  vec_assert(n, ptype = integer())
  vec_assert(d, ptype = integer())

  new_rcrd(list(n = n, d = d), class = "vctrs_rational")
}

format.vctrs_rational <- function(x, ...) {
  n <- field(x, "n")
  d <- field(x, "d")

  out <- paste0(n, "/", d)
  out[is.na(n) | is.na(d)] <- NA

  out
}

到目前为止一切都很好,但是当我尝试使用 dplyr::mutate 创建一列有理数时,出现错误

df <- data.frame(n = c(1L, 2L, 3L), d = 2L)
df %>% dplyr::mutate(frac = new_rational(n, d))
#> Error: Column `frac` must be length 3 (the number of rows) or one, not 2

但是在基础 R 中创建列就可以了:

df$rational <- new_rational(df$n, df$d)
df
#>   n d rational
#> 1 1 2      1/2
#> 2 2 2      2/2
#> 3 3 2      3/2

是否有一些技巧可以使用 dplyr::mutate 使其工作,或者这是不可能的?

标签: rdplyrvctrs

解决方案


new_rational以列表格式返回输出,如下所示

> typeof(new_rational(n=1L, d=2L))
[1] "list"

map因此,我们可以使用或as.list“@Ronak 的建议”将输出作为列表获取,然后使用unnest.

df %>% dplyr::mutate(frac = purrr::map2(n,d, ~new_rational(.x, .y))) %>% 
       tidyr::unnest(cols=c(frac))
# A tibble: 3 x 3
      n     d       frac
  <int> <int> <vctrs_rt>
1     1     2        1/2
2     2     2        2/2
3     3     2        3/2

推荐阅读