基于整行,在R中使用dplyr / magrittr过滤行

编程入门 行业动态 更新时间:2024-10-27 16:33:13
本文介绍了基于整行,在R中使用dplyr / magrittr过滤行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

一个可以使用 filter 使用dplyr过滤行,但条件通常基于每行的特定列,例如

One is able to filter rows with dplyr with filter, but the condition is usually based on specific columns per row such as

d <- data.frame(x=c(1,2,NA),y=c(3,NA,NA),z=c(NA,4,5)) d %>% filter(!is.na(y))

我想通过NA的数量是否大于50%来过滤这个行,例如

I want to filter the row by whether the number of NA is greater than 50%, such as

d %>% filter(mean(is.na(EACHROW)) < 0.5 )

推荐答案

您可以使用 rowSums 为了那个原因。提供的数据的示例:

You could use rowSums for that. An example with the provided data:

> d x y z 1 1 3 NA 2 2 NA 4 3 NA NA 5 d %>% filter(rowSums(is.na(.))/ncol(.) < 0.5) # or: d %>% filter(rowMeans(is.na(.)) < 0.5)

其中:

x y z 1 1 3 NA 2 2 NA 4

正如你可以看到第3行从数据中删除。

As you can see row 3 is removed from the data.

在基数R中,您可以执行以下操作:

In base R, you could just do:

d[rowMeans(is.na(d)) < 0.5,]

获得相同的结果。

更多推荐

基于整行,在R中使用dplyr / magrittr过滤行

本文发布于:2023-07-26 07:07:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1214874.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:dplyr   magrittr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!