mardi 22 juin 2021

Finding an alternative to using if / else to enable multiple elements to be used within a pipe

I have seven years of temperature data recorded every hour for about 9 weeks. However two of the years have recorded every half an hour. For those two years, I only want to use every other line, hence hourly data. Using an if / else statement gives the following warning:

Warning message:In if (.$Year == 2015 | .$Year == 2016) { : the condition has length > 1 and only the first element will be used

So I have tried using an ifelse statement, but now I get an error message:

Error in ifelse(., .$Year == 2015 | .$Year == 2016, subset(row_number()%%2 == : (list) object cannot be coerced to type 'logical'

Can anyone offer any other suggestions/ help?

This the beginning of my code:

  FloodedNestsIncSub = group_by (TempData, NestID, Nest, Year) %>%
  ifelse (.$Year == 2015 | .$Year == 2016, subset(row_number() %% 2 == 1) ) %>%
  mutate(TempDrop = Temp - lag(Temp, n=1, default = first(Temp))) %>%
  mutate(Flooded = TempDrop < -0.45)

If I add a comma after "subset(row_number() %% 2 == 1", and leave it blank - the else is simply to go to the next line, I get a different error:

Error in ifelse(., .$Year == 2015 | .$Year == 2016, subset(row_number()%%2 ==  : 
  unused argument (alist())

I need code for If the year is 2015 or 2016 skip every other line, then go to the next line, else, just go to the nest line ...

Aucun commentaire:

Enregistrer un commentaire