r - (list) cannot be coerced to double within a function with dplyr -


i trying filter out outliers data. here head data:

   x tile resolution topo max_height mean_height rugosity vertical_diversity  openness 1  9    1          5 high   19.41845   10.244725 5.533991          0.7931086 10.049369 2 21   10          5 high   22.49638   13.684197 5.707197          0.7863063  5.613547 3 33   11          5 high   16.96429    9.886287 4.190133          0.7471749 11.596492 4 45   12          5 high   19.83373   10.516013 5.297644          0.8006827  6.116738 5 57   13          5 high   21.69734   12.872438 6.009529          0.7797255  5.602573 6 69   14          5 high   23.77329   15.121994 5.777413          0.7651389  4.814283 

when used following code:

lidar_summary_5_max_height_outlier_filter <- lidar_summary_5 %>%     filter(max_height <= mean(max_height)+2*sd(max_height) | max_height >= mean(max_height) - 2*sd(max_height)) 

i results looking for. however, since doing several iterations, trying create function can pass in each of several variables max_height, mean_height, rugosity, vertical_diversity, openness , different data files into. made following function:

outlier_filter <- function(dat, col){   dat %>%      dplyr::filter(col <= (mean(col)+2*sd(col)) | col >= (mean(col) - 2*sd(col))) } 

where dat name of data frame , col variable removing outliers from. when run function, following error:

 error in is.data.frame(x) :   (list) object cannot coerced type 'double'  

and warning:

in addition: warning message: in mean.default(col) : argument not numeric or logical: returning na 

i have tried these errors separately, solutions found did not in case. i'm not sure what's happening since method works fine outside of function not work in function.

the col argument in outlier_filter function needs evaluated in non-standard context. here's 1 way that, along lines of programming dplyr vignette:

outlier_filter <- function(dat, col){   col <- enquo(col)   dat %>%      dplyr::filter(!!col <= (mean(!!col)+2*sd(!!col)) | !!col >= (mean(!!col) - 2*sd(!!col))) } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -