使用2列和R中的条件对数据帧进行子集

使用2列和R中的条件对数据帧进行子集,r,dataframe,dplyr,R,Dataframe,Dplyr,我有这样一个数据帧: 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 ID我们也可以 df[with(df, !(PT&g

我有这样一个数据帧:

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
ID我们也可以

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

你在找
df[!is.na(df$Measurement)| df$PT
?@josilber,太棒了。这正是我想要的。我把你的解决方案带到了我更大的数据集中,效果很好。非常感谢:-)还需要dplyr解决方案。谢谢你,阿克伦。
  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
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
library(dplyr)
df %>%
   filter(!(PT > Limit & is.na(Measurement)))