Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打印由dplyr';过滤掉的行数;s滤波函数_R_Printing_Filter_Dplyr - Fatal编程技术网

打印由dplyr';过滤掉的行数;s滤波函数

打印由dplyr';过滤掉的行数;s滤波函数,r,printing,filter,dplyr,R,Printing,Filter,Dplyr,是否有一种方法可以使用dplyr的filter函数从数据帧打印每个filter操作筛选的行数 考虑一个经过筛选的简单示例数据帧: test.df <- data.frame(col1 = c(1,2,3,4,4,5,5,5)) filtered.df <- test.df %>% filter(col1 != 4, col1 != 5) test.df非常接近!你实际上是在找关于这个的章节 库(dplyr) 打印过滤的行使用:Species!=“维吉尼亚” #>[1]萼片。

是否有一种方法可以使用dplyr的filter函数从数据帧打印每个filter操作筛选的行数

考虑一个经过筛选的简单示例数据帧:

test.df <- data.frame(col1 = c(1,2,3,4,4,5,5,5))

filtered.df <- test.df %>% filter(col1 != 4, col1 != 5)

test.df非常接近!你实际上是在找关于这个的章节

库(dplyr)
打印过滤的行使用:Species!=“维吉尼亚”
#>[1]萼片。长萼片。宽花瓣。长花瓣。宽种
#>(或长度为0的行名称)

功能强大。但是,虽然它在dplyr 0.4上工作,但在dplyr 0.7中不工作。另外,请看一看purr::walk(),它将打印函数的副作用并通过管道传输数据。
print_filtered_rows <- function(dataframe, ...) {
        dataframe_new <- dataframe
        for(arg in list(...)) {
            print(arg)
            dataframe <- dataframe_new
            dataframe_new <- dataframe %>% filter(arg)
            rows_filtered <- nrow(dataframe) - nrow(data_fram_new)
            print(sprintf('Filtered out %s rows using: %s', rows_filtered, arg)
        }
    return(dataframe_new)
}
library(dplyr)

print_filtered_rows <- function(dataframe, ...) {
  df <- dataframe
  vars = as.list(substitute(list(...)))[-1L]
  for(arg in vars) {
    dataframe <- df
    dataframe_new <- dataframe %>% filter(arg)
    rows_filtered <- nrow(df) - nrow(dataframe_new)
    cat(sprintf('Filtered out %s rows using: %s\n', rows_filtered, deparse(arg)))
    df = dataframe_new
  }
  return(dataframe_new)
}

data(iris)

iris %>% 
  print_filtered_rows(Species == "virginica", Species != "virginica") %>% 
  head()
#> Filtered out 100 rows using: Species == "virginica"
#> Filtered out 50 rows using: Species != "virginica"
#> [1] Sepal.Length Sepal.Width  Petal.Length Petal.Width  Species     
#> <0 rows> (or 0-length row.names)