-2

I'm working wither temperature and snow melt. I have some issues with removing specific ID values by e specific date. Here is a part of my Date. Frame:

enter image description here

I have calculated the onset of snowmelt and need to remove all dates before the first date of snowmelt at each location. The mean onset of snowmelt in the three locations is Top - 07.05.2021, Middle- 14.05.2021, and Bottom - 21.05.21

After some R-codes I would like to get this DataFrame: enter image description here

I did running this R-code:

df1 <- climate_2021 %>% group_by(Location) %>%filter(Date >= "2021.05.21" & Location == "Bottom" | Date >= "2021.05.14" & Location == "Middle" | Date >= "2021.05.07" & Location == "Top")

Then I get, an error in the date, comparison (<=) is possible only for atomic and list types.

What can I do to figure this out?

1 Answers1

0

Would recommend you edit your post and provide data (such as using dput(head(climate_2021)) instead of a screenshot).

Also, make sure you are converting the Date column to Date format, if not done already.

Next, consider creating a named vector that includes the threshold dates for top, middle, and bottom.

Here's a quick example using the first part of your data.

vec <- as.Date(c(TOP = "2021-05-07", MIDDLE = "2021-05-14"))

library(tidyverse)

climate_2021 %>%
  mutate(Date = as.Date(Date, format = "%d.%m.%Y")) %>%
  filter(Date >= vec[Location])

Output

  Location       Date
1      TOP 2021-05-07
2      TOP 2021-05-14
3      TOP 2021-05-21
4   MIDDLE 2021-05-14
5   MIDDLE 2021-05-21

Data

climate_2021 <- structure(list(Location = c("TOP", "TOP", "TOP", "TOP", "MIDDLE", 
"MIDDLE", "MIDDLE", "MIDDLE"), Date = c("01.05.2021", "07.05.2021", 
"14.05.2021", "21.05.2021", "01.05.2021", "07.05.2021", "14.05.2021", 
"21.05.2021")), class = "data.frame", row.names = c(NA, -8L))
Ben
  • 28,684
  • 5
  • 23
  • 45