【问题标题】:dividing all columns by another将所有列除以另一列
【发布时间】:2020-12-08 00:49:23
【问题描述】:

我尝试使用 mutate across 和我的数据跟踪线程 (Divide all columns by a chosen column using mutate_all),为每个除以“PanCK”的列生成一个新列,但是收到 non-numeric argument to binary operator 错误。

>dput(counts)
structure(list(Image = c("38_restained.ome.tif", "58 RESTAIN.ome.tif", 
"64_restain.ome.tif", "B06.ome.tif", "B07.ome.tif", "B09.ome.tif", 
"B12.ome.tif", "B15.ome.tif", "B16.ome.tif", "B32.ome.tif", "B38-1.ome.tif", 
"B39.ome.tif", "B40.ome.tif", "B49.ome.tif", "B60.ome.tif", "B62.ome.tif", 
"b64.ome.tif"), PanCk = c(560558, 204008, 317311, 5585, 348636, 
582431, 357182, 391404, 323727, 592087, 423694, 177667, 379988, 
417760, 107039, 215493, 296013), `PD-L1` = c(910, 89, 121, 91, 
37900, 2782, 2495, 115978, 8159, 6705, 10179, 1228, 3781, 26278, 
1007, 4265, 1581), CD8 = c(73, 13, 22, 395, 54872, 6631, 10130, 
40904, 23442, 8010, 29402, 5122, 19098, 59773, 3207, 16607, 20973
), `PD-1` = c(5816, 1978, 1070, 1053, 43391, 8529, 8887, 40192, 
13051, 9749, 13119, 2115, 13067, 26698, 3558, 12530, 2283), FoxP3 = c(395, 
162, 100, 806, 14988, 2914, 2164, 18424, 14900, 11419, 4955, 
1173, 4271, 4929, 1022, 955, 1396), CD68 = c(6504, 522, 285, 
805, 45939, 16150, 10324, 15594, 26188, 15339, 19394, 2874, 26130, 
20470, 2631, 2636, 7390)), row.names = c(NA, -17L), groups = structure(list(
    Image = c("38_restained.ome.tif", "58 RESTAIN.ome.tif", "64_restain.ome.tif", 
    "B06.ome.tif", "B07.ome.tif", "B09.ome.tif", "B12.ome.tif", 
    "B15.ome.tif", "B16.ome.tif", "B32.ome.tif", "B38-1.ome.tif", 
    "B39.ome.tif", "B40.ome.tif", "B49.ome.tif", "B60.ome.tif", 
    "B62.ome.tif", "b64.ome.tif"), .rows = structure(list(1L, 
        2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
        15L, 16L, 17L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 17L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

尝试使用:

countnorm <- counts %>% 
  mutate(across(everything()), ./ "PanCk")

【问题讨论】:

  • mutate(across(everything(), ~(./ PanCk)))
  • mutate(across(everything(), `/`, PanCk))

标签: r dplyr


【解决方案1】:

您收到错误是因为您的第一列是字符并且您将其除以PanCk

在基础 R 中,您可以直接使用

counts[-1] <- counts[-1]/counts$PanCk

如果您对dplyr 方法感兴趣,请注意您的数据按Image 分组,因此您需要先取消分组,然后除以PanCk,忽略第一列。

library(dplyr)
counts %>% ungroup %>% mutate(across(-1, ~(./ PanCk)))

#  Image                PanCk  `PD-L1`       CD8  `PD-1`    FoxP3     CD68
#   <chr>                <dbl>    <dbl>     <dbl>   <dbl>    <dbl>    <dbl>
# 1 38_restained.ome.tif     1 0.00162  0.000130  0.0104  0.000705 0.0116  
# 2 58 RESTAIN.ome.tif       1 0.000436 0.0000637 0.00970 0.000794 0.00256 
# 3 64_restain.ome.tif       1 0.000381 0.0000693 0.00337 0.000315 0.000898
# 4 B06.ome.tif              1 0.0163   0.0707    0.189   0.144    0.144   
# 5 B07.ome.tif              1 0.109    0.157     0.124   0.0430   0.132   
# 6 B09.ome.tif              1 0.00478  0.0114    0.0146  0.00500  0.0277  
# 7 B12.ome.tif              1 0.00699  0.0284    0.0249  0.00606  0.0289  
# 8 B15.ome.tif              1 0.296    0.105     0.103   0.0471   0.0398  
# 9 B16.ome.tif              1 0.0252   0.0724    0.0403  0.0460   0.0809  
#10 B32.ome.tif              1 0.0113   0.0135    0.0165  0.0193   0.0259  
#11 B38-1.ome.tif            1 0.0240   0.0694    0.0310  0.0117   0.0458  
#12 B39.ome.tif              1 0.00691  0.0288    0.0119  0.00660  0.0162  
#13 B40.ome.tif              1 0.00995  0.0503    0.0344  0.0112   0.0688  
#14 B49.ome.tif              1 0.0629   0.143     0.0639  0.0118   0.0490  
#15 B60.ome.tif              1 0.00941  0.0300    0.0332  0.00955  0.0246  
#16 B62.ome.tif              1 0.0198   0.0771    0.0581  0.00443  0.0122  
#17 b64.ome.tif              1 0.00534  0.0709    0.00771 0.00472  0.0250  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-05
    • 2016-05-28
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多