如何从 R 中的 scale() 规范化计算中排除某些行?

How to exclude certain rows from scale() normalizing calculations in R?

我正在尝试绘制一些测序数据的图表,并希望仅从缩放 计算 中排除 4 号染色体数据(其中第一列中的行具有“4”)。 4 号染色体可能会扭曲标准化 mean/Sd 计算,因此我想将它从我的 scale() 函数中排除。有什么办法吗?现在,我有:

preMBT_RT <-preMBT_RT %>% mutate_each_(funs(scale(.) %>% as.vector),vars=c("Timing"))

^但是有什么方法可以指示在该函数中排除第一列中带有“4”的行吗? 我仍然希望新数据框具有带“4”的缩放行,我只是希望 scale() 中的计算不使用染色体 4 数据。非常感谢任何帮助 - 谢谢!

以下是数据框的简要示例:

Chromosome     Location     Replication Timing
1              3748         -0.0001
4              1847101      0.000302   <-row I would want to exclude
20             1234         0.000102
...            ...          ...

我们可以将对应于'Chromosome' 4的'Timing'替换为NA,然后进行缩放

preMBT_RT %>%
       mutate(Timing =  scale(Timing *NA^(Chromosome =="4")))

如果我们需要排除 scale 中的值,同时保留 'Timing'

的原始值
preMBT_RT %>% 
   mutate(Timing =  ifelse(Chromosome =="4", Timing, scale(Timing[Chromosome != "4"])))