2

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).

Note: However I still want to include any data that has PT > Limit but with a Measurement type in it. 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?

2
  • 3
    Are you looking for df[!is.na(df$Measurement) | df$PT < Limit,]? Commented Dec 9, 2015 at 2:28
  • @josilber, fantastic. Exactly does what I wanted. I took your solution to my bigger data set and works like charm. Thank you so much :-) Commented Dec 9, 2015 at 2:51

1 Answer 1

1

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

library(dplyr)
df %>%
   filter(!(PT > Limit & is.na(Measurement)))
Sign up to request clarification or add additional context in comments.

1 Comment

Wanted a dplyr solution as well. Thanks much akrun.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.