以下是一个示例代码,用于编写一个自定义的dplyr函数,将列表中的每个tibble按降序排序。
library(dplyr)
# 自定义函数
custom_sort <- function(list_of_tibbles, column) {
# 使用lapply循环处理每个tibble
lapply(list_of_tibbles, function(tibble) {
# 对每个tibble按降序排序
arrange(tibble, desc({{ column }}))
})
}
# 创建一个示例列表
list_of_tibbles <- list(
tibble(a = c(1, 2, 3), b = c(4, 5, 6)),
tibble(a = c(4, 3, 2), b = c(9, 8, 7)),
tibble(a = c(2, 4, 6), b = c(3, 1, 5))
)
# 调用自定义函数进行排序
sorted_list <- custom_sort(list_of_tibbles, a)
# 打印排序后的列表
print(sorted_list)
运行以上代码,将会得到按照a
列降序排序后的列表。