for循环识别R

时间:2017-06-13 13:13:53

标签: r outliers

我有一个包含85个变量的数据框,其中大约60个是数字。我想确定一次性数字列的异常值(我正在使用Outliers包),可能使用'for循环'以及对应于数据框中异常值的行。

注意:对应于异常值的行可以是>每个离群值都为1。

现在编写的代码如下。我在循环之前创建了一些适当的空向量,并将值插入到这些向量中。在执行循环之后,我已经将向量填充到新的数据帧中。

我的问题:我无法识别具有该特定异常值的数据框中的行。这可能是因为每个异常值可能存在于多于一行中。 OutlierRow2具有值,但我无法将其转换为data.frame。我请求一些人来帮助我。

## initialize null vectors 
numericVariables <- NULL
OutlierValue <- NULL
OutlierRow2 <- NULL

# for loop to identify the outlier of numeric columns of a data.frame and save it in appropriate vectors
for(col in names(Table1))
{
  if(class(Table1[,col])=='numeric')
  {
    numericVariables = c(numericVariables,col)
    OutlierValue = c(OutlierValue,outlier(Table1[,col]))
    outlierRow2 = which(Table1[,col] == OutlierValue)
  }
}

# create the Outlier Table data frame with the vector values obtained above 
OutlierTable <- data.frame(numericVariables,OutlierValue,OutlierRow2)

0 个答案:

没有答案