我最近一直在测试 vctrs 包,尤其是最近他们所谓的“记录样式”对象,我想知道是否有任何方法可以让它们与 dplyr::mutate 一起玩得很好。目前,每当我尝试使用对象时,dplyr::mutate 都会给我一个关于对象长度的错误。
我不知道有什么合适的内置类型,所以作为代表,我将使用 this vignette 中描述的rational 类。 .
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 使其工作,或者这是不可能的?
最佳答案
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
关于r - dplyr::mutate 是否适用于记录样式的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59727702/