r语言 - 在'summarise(data, lhs = rhs)'调用中:没有办法将 rhs 的属性转移到 lhs 中吗?



以下几行突出显示了这个问题。属性在许多情况下都会被转移,但在最后一种情况下则不会,这是一种常见的"总结"用例。非常感谢您的帮助!

suppressWarnings(suppressPackageStartupMessages(library(dplyr)))
obj <- c(12, 13, 51)
attributes(obj)<- list(cv = c(3, 4, 2))
print(obj)
#> [1] 12 13 51
#> attr(,"cv")
#> [1] 3 4 2
## 'obj' has an attribute
print(attributes(obj))
#> $cv
#> [1] 3 4 2
tbl <- tibble::tibble(col = obj)
print(tbl$col)
#> [1] 12 13 51
#> attr(,"cv")
#> [1] 3 4 2
## attributes are retained by the col obj was assigned to
print(attributes(tbl$col))
#> $cv
#> [1] 3 4 2
foo <- function(x){
# to be called within 'summarise()'
o <- sum(x)
attributes(o)<- list(cvv = o*2)
return(o)
}
# produces values with attributes
print(foo(7))
#> [1] 7
#> attr(,"cvv")
#> [1] 14
tbl2 <- tbl %>% 
summarise(z = foo(col))
# with one single row, attributes are transferred to tbl2
print(attributes(tbl2$z))
#> $cvv
#> [1] 152
tbl2 <- tbl %>% 
group_by(col) %>% 
summarise(z = foo(col), .groups = "keep")
# with more rows, attributes are NO longer present in tbl2
print(attributes(tbl2$z))
#> NULL

由reprex包(v0.3.0(于2021-04-25创建

谢谢,是的,Akrun你是对的,列表包装实际上保留了属性
根据我的理解,要完成答案并获得所需结果(一个带有属性的非列表列(,需要一个相当复杂的过程(请参阅代码(
是否可以在不中断"管道"流动的情况下实现相同的结果?

suppressWarnings(suppressPackageStartupMessages(library(dplyr)))
suppressWarnings(suppressPackageStartupMessages(library(purrr)))
obj <- c(12, 13, 51)
attributes(obj)<- list(cv = c(3, 4, 2))
tbl <- tibble::tibble(col = obj)
print(attributes(tbl$col))
#> $cv
#> [1] 3 4 2
foo <- function(x){
# to be called within 'summarise()'
o <- sum(x)
attributes(o)<- list(cvv = o*2)
return(o)
}
tbl2 <- tbl %>% 
group_by(col) %>% 
summarise(z = list(foo(col)), .groups = "keep")
atts <- map_df(tbl2$z, attributes)
tbl2 <- tbl2 %>% 
mutate(z = unlist(z))
attributes(tbl2$z)<- atts
# now attributes are present in tbl2!!! but the flow is somewhat twisted!
print(attributes(tbl2$z))
#> $cvv
#> [1]  24  26 102

由reprex包(v0.3.0(于2021-04-26创建

最新更新