首页 > 解决方案 > R:如何在我的函数中创建用户输入向量?

问题描述

我有这个查找异常值的功能。它工作得很好,除非我犯了错误。

在那种情况下,我需要一路回到起点。

该函数绘制数据框中每个变量的分布,并要求用户输入与异常值相关的索引。

这是函数的部分。它嵌入在 for 循环中。

    #mark the extreme outliers, the rest are reasonable outliers
    A <- colnames(df_out_id[i])

    P <- readline(prompt="Would you like to see the full table of outliers? (enter to skip): ")
    if(P == 0 | length(P) > 1){print("Reminder: the following questions identify outliers")}
    if(P == "y" | P == "Y"){View(Outliers)}

    W <- as.numeric(readline(prompt="Enter the index for first Extreme value on the lower limit (if none, enter 0): "))
    Q <- as.numeric(readline(prompt="Enter the index for final Extreme value on the upper limit (if none, enter 0): "))

    col <- df_out_id[i]
    df_out_id[i] <- sapply(col[[1]], function(x){
      if(Q>1 & x %in% Outliers$curr_column[1:Q]) return('Extreme')
      if(W>1 & x %in% Outliers$curr_column[W:length(Outliers$curr_column)]) return('Extreme')
      else if (x %in% Outliers$curr_column[Q+1:length(Outliers$curr_column)]) return('Reasonable')
      else return('Non-Outlier')
    })

  }

  #return a dataframe with outlier status, excluding the outlier ID columns
  summary(df_out_id)
  return(df_out_id[1:(length(names(df_out_id))-3)])
}

完整的函数是 GetOutliers(),在此处描述。

有没有办法将任何输入保存到向量中,然后在函数末尾打印它们?

具体来说,在函数成功完成或在此过程中中止的情况下打印它们。

标签: r

解决方案


推荐阅读