我对在r中工作非常陌生,希望在编写代码方面获得一些帮助。
下面是汇总数据框,BARCODE是每个样品处理的名称,在每个样品处理中,都有“ pos”或“ neg”的数据集,分别代表阳性和阴性对照。
如何编写代码以计算每个BARCODE中EXPERIMENT_TYPE中变量的CV%? CV%可以通过以下公式计算,即mean_pos / sd_pos * 100%
BARCODE EXPERIMENT_TYPE MEAN SD
1 ACC_MCF10A neg 1882.700 421.39194
2 ACC_MCF10A pos 158.500 60.80328
3 ACC_Rad21 neg 2219.714 1069.38612
4 ACC_Rad21 pos 134.950 81.46131
我想要的数据输出是这样的
BARCODE CV%_pos CV%_neg
1 ACC_MCF10A
2 ACC_Rad21
谢谢!任何帮助将不胜感激!!!!
答案 0 :(得分:1)
这是一个tidyverse
选项
library(tidyverse)
df %>%
group_by(BARCODE, EXPERIMENT_TYPE) %>%
summarise(CV = SD / MEAN*100) %>%
rename(EXPERIMENT_TYPE = CV,
`CV%` = EXPERIMENT_TYPE) %>% # here we (more or less) swap column names
spread(`CV%`, EXPERIMENT_TYPE, sep = "_")
# A tibble: 2 x 3
# Groups: BARCODE [2]
# BARCODE `CV%_neg` `CV%_pos`
# <chr> <dbl> <dbl>
# 1 ACC_MCF10A 22.4 38.4
# 2 ACC_Rad21 48.2 60.4
数据
df <- structure(list(BARCODE = c("ACC_MCF10A", "ACC_MCF10A", "ACC_Rad21",
"ACC_Rad21"), EXPERIMENT_TYPE = c("neg", "pos", "neg", "pos"),
MEAN = c(1882.7, 158.5, 2219.714, 134.95), SD = c(421.39194,
60.80328, 1069.38612, 81.46131)), .Names = c("BARCODE", "EXPERIMENT_TYPE",
"MEAN", "SD"), class = "data.frame", row.names = c("1", "2",
"3", "4"))