我有一个表(在Spotfire中使用R),试图根据同一表中的数据确定调整后的峰面积。因此,下面是表格的示例
df <- data.frame(Sample_Name = c("Smpl 1", "Smpl 1", "Smpl 2", "Smpl 2"),
Peak_Area = c(100, 101, 50, 51),
Analyte = c("Asn","Asn*","Leu","Leu*"),
Int_Std = c("Asn*","","Leu*",""))
因此,对于我来说,要确定调整后的峰面积,我需要通过将Int Std字段与Analyte字段匹配来找到相同样品和相同分析物名称的内标峰面积。因此,对于样本1,计算出的值为100/101。
本质上,我想查看每一行。如果它同时具有“ Analyte”和“ Int Std”字段值,我希望它查找与相同“ Sample Name”和“ Analyte” =“ Int Std”匹配的另一行,然后将原始行除以“ Peak”。面积”值由找到的“峰面积”值行(100/101)
答案 0 :(得分:0)
您可以尝试:
library(dplyr)
df %>%
mutate_if(is.factor, as.character) %>%
group_by(`Sample Name`) %>%
mutate(
`Adjusted Peak` = if (any(Analyte %in% `Int Std`)) `Peak Area`[!Analyte %in% `Int Std`] / `Peak Area`[Analyte %in% `Int Std`] else NA_real_
)
输出:
# A tibble: 4 x 5
# Groups: Sample Name [2]
`Sample Name` `Peak Area` Analyte `Int Std` `Adjusted Peak`
<chr> <dbl> <chr> <chr> <dbl>
1 Smpl 1 100 Asn Asn* 0.990
2 Smpl 1 101 Asn* "" 0.990
3 Smpl 2 50 Leu Leu* 0.980
4 Smpl 2 51 Leu* "" 0.980