简体   繁体   English

使用 2 列和 R 中的条件子集数据框

[英]Subset a dataframe using 2 columns with a condition in R

I have a dataframe like this:我有一个这样的数据框:

ID <- c("A","B","C","D","E","F","G","H","I")
Measurement <- c('Length',NA,NA,NA,'Length','Length',NA,NA,'Length')
PT <- c(27,35,38,22,35,39,7,15,33)
df <- data.frame(ID,Measurement,PT)
Limit <- 25

I am trying to subset this dataframe using the limit as the condition so that I exclude any data that has a ("PT" value > limit AND Measuremnent = NA).我正在尝试使用限制作为条件对该数据帧进行子集化,以便排除任何具有(“PT”值> 限制 AND Measuremnent = NA)的数据。

Note: However I still want to include any data that has PT > Limit but with a Measurement type in it.注意:但是我仍然希望包含任何具有 PT > 限制但其中包含测量类型的数据。 In this case, its the length.在这种情况下,它的长度。

I am trying to do it this way but I get an error我正在尝试这样做,但出现错误

df3 <- !subset(df3,df3$PT >= Limit & df3$Measurement == '')

My desired Output is我想要的输出是

  ID Measurement PT
1  A      Length 27
2  D        <NA> 22
3  E      Length 35
4  F      Length 39
5  G        <NA>  7
6  H        <NA> 15
7  I      Length 33

I know this is pretty simple but I missing the logic somewhere.我知道这很简单,但我在某处遗漏了逻辑。 Could someone point me in the right direction?有人能指出我正确的方向吗?

We can also do我们也可以做

df[with(df, !(PT> Limit & is.na(Measurement))),]
#  ID Measurement PT
#1  A      Length 27
#4  D        <NA> 22
#5  E      Length 35
#6  F      Length 39
#7  G        <NA>  7
#8  H        <NA> 15
#9  I      Length 33

Or using dplyr或者使用dplyr

library(dplyr)
df %>%
   filter(!(PT > Limit & is.na(Measurement)))

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

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