0

I have a data frame that consists of a morphological trait measured by various observers, at different dates. My goal here is to subset the data frame to remove the measurements made by one particular observer (condition 1: within factor level) before a certain date (condition 2: below given value).

Here is an example dataframe, where I would like to get rid of the measurements made by "A" before 2012-06-04:

obs <- rep(c("A", "B"), each=5)
date <- rep(c("2012-06-01", "2012-06-02","2012-06-03","2012-06-04","2012-06-05"), times=2)
size <- rnorm(10)
df <- data.frame(date, obs, size)

I tried to play with the subset function to no avail. I am sure this must be easy! Thanks!

Mehdi.K
  • 351
  • 3
  • 13
  • You can convert the strings of dates into the date format after you create the data frame by using the following `df$date – Dale Kube Jun 30 '17 at 02:10

2 Answers2

2

Maybe this with subset

subset(df, !(obs == "A" & as.Date(date) < as.Date("2012-06-04")))


#         date obs       size
#4  2012-06-04   A -0.6892431
#5  2012-06-05   A -0.8715875
#6  2012-06-01   B  0.1167215
#7  2012-06-02   B  0.9300228
#8  2012-06-03   B  0.8731350
#9  2012-06-04   B -0.7219468
#10 2012-06-05   B  0.9846479

According to the documentation :

subset - logical expression indicating elements or rows to keep

So we create a logical vector for the rows which we want to keep.

Or without subset with same logic :

df[!(df$obs == "A" & as.Date(df$date) < as.Date("2012-06-04")), ]
Ronak Shah
  • 286,338
  • 16
  • 97
  • 143
0

We can use filter from tidyverse

library(dplyr)
df %>%
   mutate(date = as.Date(date)) %>%
   filter(!(obs == "A" &  date < as.Date("2012-06-04")))
#        date obs       size
#1 2012-06-04   A  2.0358088
#2 2012-06-05   A  0.2621886
#3 2012-06-01   B  0.3747718
#4 2012-06-02   B -0.7784647
#5 2012-06-03   B  1.0913249
#6 2012-06-04   B -0.4795268
#7 2012-06-05   B  0.6139994
akrun
  • 674,427
  • 24
  • 381
  • 486