独特挑战,取代不推荐使用的游戏()

时间:2019-09-21 23:17:58

标签: r

问题:

我有一个DataFrame,它完全由数字数据类型的变量组成。我过去有一个很好的例程,可以检查DataFrame中每个变量的统计异常值,并用NA值替换所有已识别的异常值。但是,此例程利用了最近不建议使用的funs()。

研究了这个问题之后,我知道您应该可以用例如list(〜example_func())代替funs():

>funs(mean(., trim = .2), median(., na.rm = TRUE))
>
>Would become:
>
>list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))

不幸的是,这种补救措施在我的用例中不起作用。

正在运行,但现在已不推荐使用的代码:

以下代码可以正常工作,如下所示(对于具有异常值的变量,将异常值替换为NA值);但是,它会触发有关现在已弃用的funs()的警告:

> # Which variables have missing values
> sapply(training_imptd, function(x) sum(is.na(x)))
           INDEX      TARGET_WINS   TEAM_BATTING_H  TEAM_BATTING_2B  TEAM_BATTING_3B 
               0                0                0                0                0 
 TEAM_BATTING_HR  TEAM_BATTING_BB  TEAM_BATTING_SO  TEAM_BASERUN_SB  TEAM_BASERUN_CS 
               0                0              102              131              772 
TEAM_BATTING_HBP  TEAM_PITCHING_H TEAM_PITCHING_HR TEAM_PITCHING_BB TEAM_PITCHING_SO 
            2085                0                0                0              102 
 TEAM_FIELDING_E TEAM_FIELDING_DP 
               0              286 
> 
> # Identify outliers and set them to NA (NAs to be fixed in next step by mice)
> training_imptd <- training_imptd %>%
+   mutate_all(
+     funs(ifelse(. %in% boxplot.stats(training_imptd$.)$out, NA, .))
+   )
>
> Warning: funs() is soft deprecated as of dplyr 0.8.0
> Please use a list of either functions or lambdas: 
> 
>   # Simple named list: 
>   list(mean = mean, median = median)
> 
>   # Auto named with `tibble::lst()`: 
>   tibble::lst(mean, median)
> 
>   # Using lambdas
>   list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
> This warning is displayed once per session. 
>
> # Which variables have missing values (after imputing NA for outliers)
> sapply(training_imptd, function(x) sum(is.na(x)))
           INDEX      TARGET_WINS   TEAM_BATTING_H  TEAM_BATTING_2B  TEAM_BATTING_3B 
               0               32               67               15               29 
 TEAM_BATTING_HR  TEAM_BATTING_BB  TEAM_BATTING_SO  TEAM_BASERUN_SB  TEAM_BASERUN_CS 
               0              129              102              252              827 
TEAM_BATTING_HBP  TEAM_PITCHING_H TEAM_PITCHING_HR TEAM_PITCHING_BB TEAM_PITCHING_SO 
            2086              213                4               90              140 
 TEAM_FIELDING_E TEAM_FIELDING_DP 
             303              318 

应该但不能的修正代码:

根据我所读到的有关将funs()替换为list(〜example_func())的信息,我希望以下代码与上面利用funs()的代码完全一样,但不会(对于带有离群值的变量,离群值不会被NA值取代):

> # Which variables have missing values
> sapply(training_imptd, function(x) sum(is.na(x)))
           INDEX      TARGET_WINS   TEAM_BATTING_H  TEAM_BATTING_2B  TEAM_BATTING_3B 
               0                0                0                0                0 
 TEAM_BATTING_HR  TEAM_BATTING_BB  TEAM_BATTING_SO  TEAM_BASERUN_SB  TEAM_BASERUN_CS 
               0                0              102              131              772 
TEAM_BATTING_HBP  TEAM_PITCHING_H TEAM_PITCHING_HR TEAM_PITCHING_BB TEAM_PITCHING_SO 
            2085                0                0                0              102 
 TEAM_FIELDING_E TEAM_FIELDING_DP 
               0              286 
> 
> # Identify outliers and set them to NA (NAs to be fixed in next step by mice)
> training_imptd <- training_imptd %>%
+   mutate_all(
+     list(~ ifelse(. %in% boxplot.stats(training_imptd$.)$out, NA, .))
+   )
> 
> # Which variables have missing values (after imputing NA for outliers)
> sapply(training_imptd, function(x) sum(is.na(x)))
           INDEX      TARGET_WINS   TEAM_BATTING_H  TEAM_BATTING_2B  TEAM_BATTING_3B 
               0                0                0                0                0 
 TEAM_BATTING_HR  TEAM_BATTING_BB  TEAM_BATTING_SO  TEAM_BASERUN_SB  TEAM_BASERUN_CS 
               0                0              102              131              772 
TEAM_BATTING_HBP  TEAM_PITCHING_H TEAM_PITCHING_HR TEAM_PITCHING_BB TEAM_PITCHING_SO 
            2085                0                0                0              102 
 TEAM_FIELDING_E TEAM_FIELDING_DP 
               0              286 

1 个答案:

答案 0 :(得分:1)

从函数内部删除不必要的training_imptd$。代词.已经指向“当前列”,因此您可以将其直接传递给boxplot.stats()

training_imptd %>%
  mutate_all(
    ~ifelse(. %in% boxplot.stats(.)$out, NA, .)
  )