lundi 31 mai 2021

R test if value is lowest from group, add 'yes'/'no' in new column if value is lowest from group

I'm relatively new to R and running into a problem I can't seem to solve. My apologies if this question has been asked before, but answers related to 'finding lowest' I'm running into here seem to focus on extracting the lowest value, I haven't found much about using it as a condition to add new values to a column.

A simplified example of what I'm trying to achieve is below. I have a list of building names and the years they have been in use, and I want to add to the column first_year "yes" and "no" depending on if the year the building is in use is the first year or not.

building_name   year_inuse    first_year
office          2020          yes 
office          2021          no 
office          2022          no
office          2023          no 
house           2020          yes
house           2021          no
house           2022          no
house           2023          no
retail          2020          yes
retail          2021          no
retail          2022          no
retail          2023          no

I grouped the data by the building names, and now I'm thinking about doing something like:

data_new <- data %>% mutate(first_year = if_else(...., "yes", "no"))

so add a condition in the if_else that tests if the year is the lowest from the group, and if so add a yes, otherwise add a no. However, I can't seem to figure out how to do this and if this is even the best approach.

Help is much appreciated.

Aucun commentaire:

Enregistrer un commentaire