在列表中的类似结构化 df 上应用多个函数的最佳方法?
Best method of applying multiple functions over similar structured df's in a list?
我有一个名为 Profile_list
的列表对象,它结合了多个 df,它们都具有相同的列(但行数不同):
> summary(Profile_list)
Length Class Mode
Profile_19 26 data.frame list
Profile_20 26 data.frame list
Profile_21 26 data.frame list
Profile_40 26 data.frame list
Profile_41 26 data.frame list
Profile_84 26 data.frame list
Profile_92 26 data.frame list
Profile_95 26 data.frame list
Profile_98 26 data.frame list
Profile_106 26 data.frame list
Profile_135 26 data.frame list
Profile_139 26 data.frame list
我希望能够将 dplyr::select
函数应用于 select 列 Col_A
和 Col_B
,然后找到这两个的 unique
组合提取每个 df 的列,然后将这些结果分配给与 dfs 具有相同名称的新列表,Profile_list_unique_indicators
。实现此目标的最佳方法是什么?
这里是 purrr
和 map
的解决方案(只要列名在所有 data.frames
中都相同):
purrr::map(my_list, function(x) {
x %>% select(a, b) %>% group_by(a, b) %>% unique()
})
# [[1]]
# # A tibble: 3 x 2
# # Groups: a, b [3]
# a b
# <dbl> <int>
# 1 2 1
# 2 2 2
# 3 2 3
#
# [[2]]
# # A tibble: 3 x 2
# # Groups: a, b [3]
# a b
# <dbl> <int>
# 1 1 4
# 2 1 5
# 3 1 6
但我看不出与简单使用有什么区别 distinct
:
purrr::map(my_list, function(x) {
x %>% select(a, b) %>% distinct(a, b)
})
# [[1]]
# a b
# 1 2 1
# 2 2 2
# 3 2 3
#
# [[2]]
# a b
# 1 1 4
# 2 1 5
# 3 1 6
假数据:
data <- data.frame(a = rep(2, 4), b = rep(1:3, 4))
data2 <- data.frame(a = rep(1, 4), b = rep(4:6, 4))
my_list <- list(data, data2)
my_list
# [[1]]
# a b
# 1 2 1
# 2 2 2
# 3 2 3
# 4 2 1
# 5 2 2
# 6 2 3
# 7 2 1
# 8 2 2
# 9 2 3
# 10 2 1
# 11 2 2
# 12 2 3
#
# [[2]]
# a b
# 1 1 4
# 2 1 5
# 3 1 6
# 4 1 4
# 5 1 5
# 6 1 6
# 7 1 4
# 8 1 5
# 9 1 6
# 10 1 4
# 11 1 5
# 12 1 6
我有一个名为 Profile_list
的列表对象,它结合了多个 df,它们都具有相同的列(但行数不同):
> summary(Profile_list)
Length Class Mode
Profile_19 26 data.frame list
Profile_20 26 data.frame list
Profile_21 26 data.frame list
Profile_40 26 data.frame list
Profile_41 26 data.frame list
Profile_84 26 data.frame list
Profile_92 26 data.frame list
Profile_95 26 data.frame list
Profile_98 26 data.frame list
Profile_106 26 data.frame list
Profile_135 26 data.frame list
Profile_139 26 data.frame list
我希望能够将 dplyr::select
函数应用于 select 列 Col_A
和 Col_B
,然后找到这两个的 unique
组合提取每个 df 的列,然后将这些结果分配给与 dfs 具有相同名称的新列表,Profile_list_unique_indicators
。实现此目标的最佳方法是什么?
这里是 purrr
和 map
的解决方案(只要列名在所有 data.frames
中都相同):
purrr::map(my_list, function(x) {
x %>% select(a, b) %>% group_by(a, b) %>% unique()
})
# [[1]]
# # A tibble: 3 x 2
# # Groups: a, b [3]
# a b
# <dbl> <int>
# 1 2 1
# 2 2 2
# 3 2 3
#
# [[2]]
# # A tibble: 3 x 2
# # Groups: a, b [3]
# a b
# <dbl> <int>
# 1 1 4
# 2 1 5
# 3 1 6
但我看不出与简单使用有什么区别 distinct
:
purrr::map(my_list, function(x) {
x %>% select(a, b) %>% distinct(a, b)
})
# [[1]]
# a b
# 1 2 1
# 2 2 2
# 3 2 3
#
# [[2]]
# a b
# 1 1 4
# 2 1 5
# 3 1 6
假数据:
data <- data.frame(a = rep(2, 4), b = rep(1:3, 4))
data2 <- data.frame(a = rep(1, 4), b = rep(4:6, 4))
my_list <- list(data, data2)
my_list
# [[1]]
# a b
# 1 2 1
# 2 2 2
# 3 2 3
# 4 2 1
# 5 2 2
# 6 2 3
# 7 2 1
# 8 2 2
# 9 2 3
# 10 2 1
# 11 2 2
# 12 2 3
#
# [[2]]
# a b
# 1 1 4
# 2 1 5
# 3 1 6
# 4 1 4
# 5 1 5
# 6 1 6
# 7 1 4
# 8 1 5
# 9 1 6
# 10 1 4
# 11 1 5
# 12 1 6