当没有匹配条件时搜索有序的“列表”匹配条件,列表长度= 1

时间:2013-03-22 15:11:22

标签: r

我有一个包含3列的排序列表,我正在搜索第二列是否匹配2或4,然后返回第一列的元素(如果有),并将其放入函数中。

noOutliers((L1LeanList[order(L1LeanList[,1]),])[(L1LeanList[order(L1LeanList[,1]),2]==2)|
                                                (L1LeanList[order(L1LeanList[,1]),2]==4),1])

当没有任何条件匹配时。我得到了

Error in ((L1LeanList[order(L1LeanList[, 1]), ])[1, ])[(L1LeanList[order(L1LeanList[,  : 
  incorrect number of dimensions

由于我们实际上有List [List [all false]]

我不能只提出像L1LLSorted&lt ;-(L1LeanList [order(L1LeanList [,1]),] 并使用L1LLSorted [,2],因为当列表长度恰好为1

时会返回错误

所以现在我的代码需要看起来像

noOutliers(ifelse(any((L1LeanList[order(L1LeanList[,1]),2]==2)|
                      (L1LeanList[order(L1LeanList[,1]),2]==4)),0,
                      (L1LeanList[order(L1LeanList[,1]),])[(L1LeanList[order(L1LeanList[,1]),2]==2)|
                                                           (L1LeanList[order(L1LeanList[,1]),2]==4),1])))
对于我要求的简单事情,这似乎有点荒谬。 写这篇文章的时候,我意识到我最终可以把所有这些错误检查都放到noOutliers函数本身,所以它看起来像     noOutliers(L1LeanList,2,2,4)看起来会好得多,因为我的代码中略有不同的版本会出现几十次。我不禁想知道,如果有更优雅的方式来编写实际的功能。

对于好奇的人来说,noOutliers在排序数据集中找到了30 -70th百分位数的平均值

noOutliers<-function(oList)
{
if (length(oList)<=20) return ("insufficient data")
cumSum<-0
iterCount<-0
for(i in round(length(oList)*3/10-.000001):round(length(oList)*7/10+.000001)+1)#adjustments deal with .5->even number rounding r mishandling
{                                                                              #and 1-based indexing (ex. for a list 1-10, taking 3-7 cuts off 1,2,8,9,10, imbalanced.)
  cumSum<-cumSum+oList[i]
  iterCount<-iterCount+1
}
return(cumSum/iterCount)

}

1 个答案:

答案 0 :(得分:0)

让我们看看......

foo <- bar[(bar[,2]==2 | bar[,2]==4),1]

应该提取所需的所有第一列值。然后在foo上运行你想要的任何函数,或许需要警告“if(length(foo)&lt; 1)然后{exit,或skip,or something}”