R

时间:2019-02-21 01:00:02

标签: r statistics bioinformatics

我想获得一定比例的pcr检测,并使用以下测试数据集创建一个新列。我只希望在pcr1至pcr6列中为每一行进行检测。我希望忽略其他列。

  site sample pcr1 pcr2 pcr3 pcr4 pcr5 pcr6
pond 1      1 1    1    1    0    1    1
pond 1      2 1    1    0    0    1    1
pond 1      3 0    0    1    1    1    1

我希望输出使用比例检测创建一个新列。上面的数据集只是我正在使用的一个小样本。我尝试过:

data$detection.proportion <- rowMeans(subset(testdf, select = c(pcr1, pcr2, pcr3, pcr4, pcr5, pcr6)), na.rm = TRUE)

这适用于这个较小的数据集,但是我尝试了较大的数据集,但它不起作用,并且给出的比例不正确。我正在寻找一种方法来计算从pcr1到pcr6的所有1,然后将它们除以1和0的总数(我知道是6,但是我希望R能够在未输入的情况下予以识别)。

1 个答案:

答案 0 :(得分:0)

我找到了一种方法,以防万一其他人需要。我不知道这是否最有效,但对我有用。

data$detection.proportion <- length(subset(testdf, select = c(pcr1, pcr2, pcr3, pcr4, pcr5, pcr6)))
#Calculates the length of pcrs = 6

p.detection <- rowSums(data[,c(-1, -2, -3, -10)] == "1") 
#Counts the occurrences of 1 (which is detection) for each row 

data$detection.proportion <- p.detection/data$detection.proportion
#Divides the occurrences by the total number of pcrs to find the detected proportion.