2

Say I have a dataframe df as follows:

df <- structure(list(date = c("2021-10-1", "2021-10-2", "2021-10-3", 
"2021-10-4", "2021-10-5", "2021-10-6", "2021-10-7", "2021-10-8", 
"2021-10-9"), value = c(190.3, 174.9, 163.2, 168.4, 168.6, 168.2, 
163.5, 161.6, 172.9), type = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L)), class = "data.frame", row.names = c(NA, -9L))

I try to filter rows where two conditions were met (or condtions, not and):

  1. type==2
  2. type==1 and max(date).

My trial code:

df$date <- as.Date(df$date)

Method 1:

df[type==2 | date==max(df[type==1]$date)]

Out:

Error in `[.data.frame`(df, type == 2 | date == max(df[type == 1]$date)) : 
object 'type' not found

Method 2:

df %>%
  filter(type==2|date==max(df[type==1]$date))

Out:

Error: Problem with `filter()` input `..1`.
i Input `..1` is `type == 3 | date == max(df[type == 2]$date)`.
x undefined columns selected

But it works out when I use in code geom_point(data=df[type==3 | date==max(df[type==2]$date)],size=2, aes(shape=type)) from this link.

The expected result:

enter image description here

I am wondering how could I filter correctly using two methods above? Thanks.

ah bon
  • 9,293
  • 12
  • 65
  • 148

1 Answers1

1

Please see if this generates the expected output.

library(dplyr)

df2 <- df %>%
  mutate(date = as.Date(date)) %>%
  filter(type == 2 | (type == 1 & date == max(date[type == 1])))
df2
#         date value type
# 1 2021-10-05 168.6    1
# 2 2021-10-06 168.2    2
# 3 2021-10-07 163.5    2
# 4 2021-10-08 161.6    2
# 5 2021-10-09 172.9    2
www
  • 38,575
  • 12
  • 48
  • 84
  • Yes, it works, I see we need use `type == 1` two times for the second condition. – ah bon Dec 14 '21 at 08:52
  • Thanks a lot, for the first method? `df[type == 2 | (type == 1 & date == max(date[type == 1]))]` seems not working and generate same error. – ah bon Dec 14 '21 at 08:54
  • 2
    @ahbon for your first method you need to anchor the data frame on each variable and also add the comma at the end to specify rows, i.e. `df[df$type == 2 | (df$type == 1 & df$date == max(df$date[df$type == 1])),]` – Sotos Dec 14 '21 at 09:06
  • OK, thanks for your help, but I don't understand why for `geom_point(data=df[type==3 | date==max(df[type==2]$date)],size=2, aes(shape=type))`, why it doesn't need to do so. – ah bon Dec 14 '21 at 09:16