如何计算数组中的值

时间:2012-10-25 12:15:24

标签: r count

如何计算多集中的值? 我有一个数据集,我想为频率表编写一个函数。所以我现在在我的数据中存在哪些异常。数据是一个数组,如下所示:

             GSM288217   GSM288219 GSM288221
ch1_500001   NA          NA        NA       
ch1_1500001  "Imbalance" "Balance" "Balance"
ch1_2500001  "Imbalance" "Balance" "Balance"
ch1_3500001  "Imbalance" "LOH"     "Balance"
ch1_4500001  "Imbalance" "LOH"     "Balance"
ch1_5500001  "Imbalance" "LOH"     "Balance"
ch1_6500001  "Imbalance" "LOH"     "Balance"
ch1_7500001  "Imbalance" "LOH"     "Balance"
ch1_8500001  "Imbalance" "LOH"     "Balance"
ch1_9500001  "Imbalance" "LOH"     "Balance"
ch1_10500001 "Imbalance" "LOH"     "Balance"
ch1_11500001 "Imbalance" "LOH"     "Balance"
ch1_12500001 "Imbalance" "LOH"     "Balance"
ch1_13500001 "Imbalance" "LOH"     "Balance"
ch1_14500001 "Imbalance" "LOH"     "Balance"
ch1_15500001 "Imbalance" "LOH"     "Balance"
ch1_16500001 "Imbalance" "LOH"     "Balance"
ch1_17500001 "Imbalance" "LOH"     "Balance"
ch1_18500001 "Imbalance" "LOH"     "Balance"
ch1_19500001 "Imbalance" "LOH"     "Balance"

现在我希望该功能每个ROW检查数据中存在多少异常。我开始时:

detection<- function(abnormality) {
  freqcount<-rep(0,nrow(abnormality))
  if(assayDataElement(abnormality, 1)=="LOH")#TRUE & FALSE?? Need frequency
    {freqcount<-freqcount+1
     }          
  return(freqcount)

}

因此,每当数据行中存在LOH时,它应该给出该行中存在的LOH数。

1 个答案:

答案 0 :(得分:3)

我想你需要

length(which(assayDataElement(abnormality, 1)=="LOH"))

或单独为每一行:

count <- function(x,what) length(which(x==what));
apply(assayDataElement(abnormality, 1),1,count,"LOH");