简体   繁体   English

如果没有满足条件的行,则返回空数据框

[英]empty data frame returned if there are no rows which meet the condition

I have a set of tables and the data in them changes. 我有一组表,它们中的数据发生了变化。 I need to remove some specific rows if there pass a condition, however in some cases, there are no rows to be removed. 如果有条件,我需要删除一些特定的行,但是在某些情况下,没有要删除的行。 Instead of getting the whole data frame back I get an empty one. 我没有得到整个数据框架,而是得到了一个空的框架。 Here is a simplified example: 这是一个简化的示例:

> data <- c(1,2,3,4,5,6,7)
> a <- data.frame(data)
> b <- a[-(a$data==6),]
> b
[1] 2 3 4 5 6 7
> b <- a[-(a$data==8),]
> b
numeric(0)

How should I remove the lines, so this doesn't happen? 我应该如何删除行,这样才不会发生?

It should be like this: 应该是这样的:

a[!(a$data==8),]

You may also want to use drop=FALSE to ensure the result is a data.frame : 您可能还需要使用drop=FALSE来确保结果为data.frame

> a[!(a$data==6),]
[1] 1 2 3 4 5 7

> a[!(a$data==6),,drop=FALSE]
  data
1    1
2    2
3    3
4    4
5    5
7    7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM