从两个 Dataframe 按组减去向量

dwbf0jvd  于 2023-03-15  发布在  其他
关注(0)|答案(3)|浏览(95)

我在R中有两个 Dataframe 。第一个 Dataframe 包含几个列-特性,以及一个列,该列说明特定样本是否(行)属于某个组(因子变量)。第二 Dataframe 包含相同数目的列,并且行数等于唯一组的数目。我想要从第一 Dataframe 的每个样本减去来自第二 Dataframe 的对应向量,其中使用相同名称的列中的键组来指定对应关系。
以下是主数据集的示例:

df_repr <- structure(list(f1 = c(-3.9956064225704, 
-0.52380279948658, 0.61089389331505, -3.47273625634875, -4.486918671214, 
-6.1761970731672, -4.62305749757367, -4.42540643005429, -3.61613137597131, 
-3.29821425516253), f2 = c(-1.57918114753228, 
-4.10523012500727, -1.80270009366593, -0.00905317702835884, -0.899585192079915, 
-2.89341515186212, 0.0132542126386332, -3.32639898550135, -0.867793877742314, 
0.0911950321630834), f3 = c(-6.02532301769732, 
-4.90073348094302, -3.73159604513274, -3.55290209472808, -6.63194560195811, 
2.69409789701296, -4.17675978927128, -3.84141885970095, -1.20571283849034, 
1.54287440902102), group = structure(c(1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor")), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -10L))

下面是一个示例 Dataframe ,其中向量将从第一 Dataframe 的对应组的每行中减去:

to_subtract <- structure(list(group = structure(1:2, .Label = c("A", 
"B"), class = "factor"), f1 = c(-2.78048744402161, 
-2.33583431665818), f2 = c(-2.56086962108741, 
-0.689157827347865), f3 = c(-3.60224982918457, 
-0.782365376308658)), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"))

# # A tibble: 2 × 4
#   group    f1     f2     f3
#   <fct> <dbl>  <dbl>  <dbl>
# 1 A     -2.78 -2.56  -3.60
# 2 B     -2.34 -0.689 -0.782

我试着这样做:

df_repr %>%
  group_by(group) %>%
  mutate(across(where(is.numeric),
         ~ . - to_subtract[to_subtract$group == unique(.$group), -1]))

但我得到了以下错误:

Error in `mutate()`:
ℹ️ In argument: `across(...)`.
ℹ️ In group 1: `group = A`.
Caused by error in `across()`:
! Can't compute column `f1`.
Caused by error in `f1$group`:
! $ operator is invalid for atomic vectors

此示例的预期输出:

f1     f2      f3 group
    <dbl>  <dbl>   <dbl> <fct>
 1 -1.22   0.982 -2.42   A    
 2  2.26  -1.54  -1.30   A    
 3  3.39   0.758 -0.129  A    
 4 -0.692  2.55   0.0493 A    
 5 -1.71   1.66  -3.03   A    
 6 -3.84  -2.20   3.48   B    
 7 -2.29   0.702 -3.39   B    
 8 -2.09  -2.64  -3.06   B    
 9 -1.28  -0.179 -0.423  B    
10 -0.962  0.780  2.33   B
5hcedyr0

5hcedyr01#

您可以将powerjoin(conflict =-)一起使用:

library(powerjoin)

power_left_join(df_repr, to_subtract, by = "group", conflict = `-`)

# A tibble: 10 × 4
   group     f1     f2      f3
   <fct>  <dbl>  <dbl>   <dbl>
 1 A     -1.22   0.982 -2.42
 2 A      2.26  -1.54  -1.30  
 3 A      3.39   0.758 -0.129
 4 A     -0.692  2.55   0.0493
 5 A     -1.71   1.66  -3.03
 6 B     -3.84  -2.20   3.48
 7 B     -2.29   0.702 -3.39
 8 B     -2.09  -2.64  -3.06  
 9 B     -1.28  -0.179 -0.423
10 B     -0.962  0.780  2.33

另一种dplyr::group_modify方法:

df_repr %>%
  group_by(group) %>%
  group_modify(~ mutate(.x, across(f1:f3, \(val) {
    val - filter(to_subtract, group == .y$group)[[cur_column()]]
  }))) %>%
  ungroup()
w8f9ii69

w8f9ii692#

您可以将目标 Dataframe 与to_subtract组合在一起,同时设置一个逻辑列来指示要从哪个 Dataframe 中减去,然后在mutate中执行减法,并重新塑造为您想要的格式。
要使用mutate(.by)函数,需要dplyr版本〉= 1.1.0。如果没有,请在使用mutate之前使用传统的group_by(group)方法。

library(dplyr)

rbind(to_subtract %>% mutate(target = T), df_repr %>% mutate(target = F)) %>% 
  mutate(across(where(is.numeric), ~ .x - .x[target]), .by = group) %>% 
  filter(!target) %>% 
  select(-target)

# A tibble: 10 × 4
   group     f1     f2      f3
   <fct>  <dbl>  <dbl>   <dbl>
 1 A     -1.22   0.982 -2.42  
 2 A      2.26  -1.54  -1.30  
 3 A      3.39   0.758 -0.129 
 4 A     -0.692  2.55   0.0493
 5 A     -1.71   1.66  -3.03  
 6 B     -3.84  -2.20   3.48  
 7 B     -2.29   0.702 -3.39  
 8 B     -2.09  -2.64  -3.06  
 9 B     -1.28  -0.179 -0.423 
10 B     -0.962  0.780  2.33
vxbzzdmp

vxbzzdmp3#

另一种方法是使用group_modify()并执行data.frame操作,为此,to_subtractdf_rep的行号必须匹配,这就是为什么我们复制to_substract中每个组的每一行以匹配df_rep

library(dplyr)

df_repr %>%
  group_by(group) %>% 
  group_modify(\(df, grp) {
    # get current group in `to_subtract` and drop `group` column
    df2 <- to_subtract[to_subtract$group == grp$group, -1]
    # match row numbers of `df` and  substract
    df - df2[rep(1, nrow(df)), ]
  })
#> # A tibble: 10 × 4
#> # Groups:   group [2]
#>    group     f1     f2      f3
#>    <fct>  <dbl>  <dbl>   <dbl>
#>  1 A     -1.22   0.982 -2.42  
#>  2 A      2.26  -1.54  -1.30  
#>  3 A      3.39   0.758 -0.129 
#>  4 A     -0.692  2.55   0.0493
#>  5 A     -1.71   1.66  -3.03  
#>  6 B     -3.84  -2.20   3.48  
#>  7 B     -2.29   0.702 -3.39  
#>  8 B     -2.09  -2.64  -3.06  
#>  9 B     -1.28  -0.179 -0.423 
#> 10 B     -0.962  0.780  2.33

数据来自OP

df_repr <- structure(list(f1 = c(-3.9956064225704, 
                                 -0.52380279948658, 0.61089389331505, -3.47273625634875, -4.486918671214, 
                                 -6.1761970731672, -4.62305749757367, -4.42540643005429, -3.61613137597131, 
                                 -3.29821425516253), f2 = c(-1.57918114753228, 
                                                            -4.10523012500727, -1.80270009366593, -0.00905317702835884, -0.899585192079915, 
                                                            -2.89341515186212, 0.0132542126386332, -3.32639898550135, -0.867793877742314, 
                                                            0.0911950321630834), f3 = c(-6.02532301769732, 
                                                                                        -4.90073348094302, -3.73159604513274, -3.55290209472808, -6.63194560195811, 
                                                                                        2.69409789701296, -4.17675978927128, -3.84141885970095, -1.20571283849034, 
                                                                                        1.54287440902102), group = structure(c(1L, 1L, 1L, 1L, 1L, 
                                                                                                                               2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor")), class = c("tbl_df", "tbl", 
                                                                                                                                                                                                        "data.frame"), row.names = c(NA, -10L))

to_subtract <- structure(list(group = structure(1:2, .Label = c("A", 
                                                                "B"), class = "factor"), f1 = c(-2.78048744402161, 
                                                                                                -2.33583431665818), f2 = c(-2.56086962108741, 
                                                                                                                           -0.689157827347865), f3 = c(-3.60224982918457, 
                                                                                                                                                       -0.782365376308658)), row.names = c(NA, -2L), class = c("tbl_df", 
                                                                                                                                                                                                               "tbl", "data.frame"))

创建于2023年3月9日,使用reprex v2.0.2

相关问题