是否有可能将向量中的数字分别提升到r中特定列的幂?

kknvjkwl  于 2023-05-11  发布在  其他
关注(0)|答案(2)|浏览(88)

我想按某些列的顺序(序列)对向量中的每个值取幂,并在每个值的新列中输入结果。我的数据是:

df6 <- structure(list(Samples = c("S1", "S1", "S1", "S2", "S2", "S2", 
"S3", "S3", "S3", "S4", "S4", "S4"), GOI_A_Ct = c(33.67, 33.67, 
33.67, 19.5, 19.5, 19.5, 16.03, 16.03, 16.03, 13, 13, 13), GOI_B_Ct = c(30.46, 
30.34, 30.58, 30.34, 30.5, 30.43, 27.06, 27.03, 27.03, 27.1, 
26.99, 27.045), Mean_REF_Ct = c(25.1553175293018, 25.0150674594333, 
24.9249052154667, 25.1052285390912, 25.1452997595972, 25.1352819757408, 
24.2135829649393, 24.0632936232761, 24.0733130250076, 24.0532742053967, 
24.0632936232761, 24.2135829649393), delta_GOI_A_Ct = c(0, 0, 
0, -14.17, -14.17, -14.17, -17.64, -17.64, -17.64, -20.67, -20.67, 
-20.67), delta_GOI_B_Ct = c(0, -0.120000000000001, 0.119999999999997, 
-0.120000000000001, 0.0399999999999991, -0.0300000000000011, 
-3.4, -3.43, -3.43, -3.36, -3.47, -3.415), delta_Mean_REF_Ct = c(0.123554127901208, 
-0.0166959419673027, -0.106858185933909, 0.0734651376905866, 
0.113536358196594, 0.103518574340168, -0.81818043646129, -0.968469778124522, 
-0.958450376393031, -0.978489196003963, -0.968469778124522, -0.81818043646129
)), class = "data.frame", row.names = c(NA, -12L))

我的vector是:
Effi<- c(1.9, 1.8, 1.2)
我的代码是:
df7 <- df6 %>% mutate(across(starts_with("delta"), function(x) (Effi^-x), .names = "{sub ('delta', 'FC', .col)}"))
但这段代码并不是按顺序工作的。在Effi向量中,我需要1.9的delta_GOI_A_Ct列的幂、1.8的delta_GOI_B_Ct列的幂和1.2的delta_Mean_REF_Ct列的幂。
我的错误结果是:

structure(list(Samples = c("S1", "S1", "S1", "S2", "S2", "S2", 
"S3", "S3", "S3", "S4", "S4", "S4"), GOI_A_Ct = c(33.67, 33.67, 
33.67, 19.5, 19.5, 19.5, 16.03, 16.03, 16.03, 13, 13, 13), GOI_B_Ct = c(30.46, 
30.34, 30.58, 30.34, 30.5, 30.43, 27.06, 27.03, 27.03, 27.1, 
26.99, 27.045), Mean_REF_Ct = c(25.1553175293018, 25.0150674594333, 
24.9249052154667, 25.1052285390912, 25.1452997595972, 25.1352819757408, 
24.2135829649393, 24.0632936232761, 24.0733130250076, 24.0532742053967, 
24.0632936232761, 24.2135829649393), delta_GOI_A_Ct = c(0, 0, 
0, -14.17, -14.17, -14.17, -17.64, -17.64, -17.64, -20.67, -20.67, 
-20.67), delta_GOI_B_Ct = c(0, -0.120000000000001, 0.119999999999997, 
-0.120000000000001, 0.0399999999999991, -0.0300000000000011, 
-3.4, -3.43, -3.43, -3.36, -3.47, -3.415), delta_Mean_REF_Ct = c(0.123554127901208, 
-0.0166959419673027, -0.106858185933909, 0.0734651376905866, 
0.113536358196594, 0.103518574340168, -0.81818043646129, -0.968469778124522, 
-0.958450376393031, -0.978489196003963, -0.968469778124522, -0.81818043646129
), FC_GOI_A_Ct = c(1, 1, 1, 8911.24790256821, 4142.01242480709, 
13.2433621854489, 82644.4170845227, 31842.4877218358, 24.932003562752, 
577879.025429365, 189009.085351031, 43.3187938492919), FC_GOI_B_Ct = c(1, 
1.07308148257159, 0.978359013524065, 1.08006634125842, 0.976762774460425, 
1.00548463253129, 8.86670466809862, 7.50904474999272, 1.86892414131498, 
8.64195696320214, 7.68768522545387, 1.86381994664705), FC_Mean_REF_Ct = c(0.923759338765905, 
1.00986196383865, 1.01967357423325, 0.953940589982091, 0.935442913732288, 
0.981303324786334, 1.69071631258144, 1.76694774635023, 1.19094387468592, 
1.87394735861131, 1.76694774635023, 1.16087256193282)), class = "data.frame", row.names = c(NA, 
-12L))
qnzebej0

qnzebej01#

使用一个命名的vector和cur_column(),你可以做到:

library(dplyr, warn=FALSE)

Effi <- c(1.9, 1.8, 1.2)
names(Effi) <- c("delta_GOI_A_Ct", "delta_GOI_B_Ct", "delta_Mean_REF_Ct")

df6 %>%
  mutate(across(starts_with("delta"),
    function(x) (Effi[[cur_column()]]^-x),
    .names = "{sub ('delta', 'FC', .col)}"
  ))
#>    Samples GOI_A_Ct GOI_B_Ct Mean_REF_Ct delta_GOI_A_Ct delta_GOI_B_Ct
#> 1       S1    33.67   30.460    25.15532           0.00          0.000
#> 2       S1    33.67   30.340    25.01507           0.00         -0.120
#> 3       S1    33.67   30.580    24.92491           0.00          0.120
#> 4       S2    19.50   30.340    25.10523         -14.17         -0.120
#> 5       S2    19.50   30.500    25.14530         -14.17          0.040
#> 6       S2    19.50   30.430    25.13528         -14.17         -0.030
#> 7       S3    16.03   27.060    24.21358         -17.64         -3.400
#> 8       S3    16.03   27.030    24.06329         -17.64         -3.430
#> 9       S3    16.03   27.030    24.07331         -17.64         -3.430
#> 10      S4    13.00   27.100    24.05327         -20.67         -3.360
#> 11      S4    13.00   26.990    24.06329         -20.67         -3.470
#> 12      S4    13.00   27.045    24.21358         -20.67         -3.415
#>    delta_Mean_REF_Ct FC_GOI_A_Ct FC_GOI_B_Ct FC_Mean_REF_Ct
#> 1         0.12355413       1.000   1.0000000      0.9777252
#> 2        -0.01669594       1.000   1.0730815      1.0030487
#> 3        -0.10685819       1.000   0.9318957      1.0196736
#> 4         0.07346514    8911.248   1.0730815      0.9866950
#> 5         0.11353636    8911.248   0.9767628      0.9795127
#> 6         0.10351857    8911.248   1.0177900      0.9813033
#> 7        -0.81818044   82644.417   7.3777939      1.1608726
#> 8        -0.96846978   82644.417   7.5090447      1.1931214
#> 9        -0.95845038   82644.417   7.5090447      1.1909439
#> 10       -0.97848920  577879.025   7.2063544      1.1953030
#> 11       -0.96846978  577879.025   7.6876852      1.1931214
#> 12       -0.81818044  577879.025   7.4431300      1.1608726
czq61nw1

czq61nw12#

如果base R方法可以,使用Map,为了清楚起见,还将使用的 Effi 值放在列名中。

Names <- grep("^delta", colnames(df6), value=T)

setNames(data.frame(df6, 
    Map(function(y, x) x^-y, 
      df6[,Names], Effi)), 
  c(colnames(df6), paste0(sub("delta", "FC", Names), "_", Effi)))
   Samples GOI_A_Ct GOI_B_Ct Mean_REF_Ct delta_GOI_A_Ct delta_GOI_B_Ct
1       S1    33.67   30.460    25.15532           0.00          0.000
2       S1    33.67   30.340    25.01507           0.00         -0.120
3       S1    33.67   30.580    24.92491           0.00          0.120
4       S2    19.50   30.340    25.10523         -14.17         -0.120
5       S2    19.50   30.500    25.14530         -14.17          0.040
6       S2    19.50   30.430    25.13528         -14.17         -0.030
7       S3    16.03   27.060    24.21358         -17.64         -3.400
8       S3    16.03   27.030    24.06329         -17.64         -3.430
9       S3    16.03   27.030    24.07331         -17.64         -3.430
10      S4    13.00   27.100    24.05327         -20.67         -3.360
11      S4    13.00   26.990    24.06329         -20.67         -3.470
12      S4    13.00   27.045    24.21358         -20.67         -3.415
   delta_Mean_REF_Ct FC_GOI_A_Ct_1.9 FC_GOI_B_Ct_1.8 FC_Mean_REF_Ct_1.2
1         0.12355413           1.000       1.0000000          0.9777252
2        -0.01669594           1.000       1.0730815          1.0030487
3        -0.10685819           1.000       0.9318957          1.0196736
4         0.07346514        8911.248       1.0730815          0.9866950
5         0.11353636        8911.248       0.9767628          0.9795127
6         0.10351857        8911.248       1.0177900          0.9813033
7        -0.81818044       82644.417       7.3777939          1.1608726
8        -0.96846978       82644.417       7.5090447          1.1931214
9        -0.95845038       82644.417       7.5090447          1.1909439
10       -0.97848920      577879.025       7.2063544          1.1953030
11       -0.96846978      577879.025       7.6876852          1.1931214
12       -0.81818044      577879.025       7.4431300          1.1608726

相关问题