R 测试值是否是组中最低的,如果值是组中最低的,则在新列中添加 'yes'/'no'

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

我是 R 的新手,运行 遇到了一个我似乎无法解决的问题。如果之前有人问过这个问题,我很抱歉,但是与 'finding lowest' I'm 运行 相关的答案似乎专注于提取最低值,我没有找到太多关于将其用作条件的信息向列添加新值。

下面是我要实现的目标的简化示例。我有一个建筑物名称列表和它们使用的年份,我想在 first_year 列中添加“是”和“否”,具体取决于建筑物使用的年份是否是第一年与否。

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

我按建筑物名称对数据进行了分组,现在我正在考虑做类似的事情:

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

所以在 if_else 中添加一个条件,测试年份是否是组中最低的,如果是,则添加是,否则添加否。但是,我似乎不知道该怎么做,也不知道这是否是最好的方法。

非常感谢您的帮助。

如果 'year_inuse' 未排序,请在执行此操作之前使用 arrangearrange by 'building_name'、'year_inuse',使用 duplicated,将其转换为数字索引 (1 + ),然后使用该索引替换为值向量,即 'yes'、'no'

library(dplyr)
data_new <- data %>%
        arrange(building_name, year_inuse) %>%
        mutate(first_year = c("no", "yes")[1 + !duplicated(building_name)])

-输出

#    building_name year_inuse first_year
#1          house       2020        yes
#2          house       2021         no
#3          house       2022         no
#4          house       2023         no
#5         office       2020        yes
#6         office       2021         no
#7         office       2022         no
#8         office       2023         no
#9         retail       2020        yes
#10        retail       2021         no
#11        retail       2022         no
#12        retail       2023         no

数据

data <- structure(list(building_name = c("office", "office", "office", 
"office", "house", "house", "house", "house", "retail", "retail", 
"retail", "retail"), year_inuse = c(2020L, 2021L, 2022L, 2023L, 
2020L, 2021L, 2022L, 2023L, 2020L, 2021L, 2022L, 2023L)),
 row.names = c(NA, 
-12L), class = "data.frame")

分组后,您可以获得该组的 min 值,并在比较中使用它,如下所示:

library(dplyr)
data <- tibble::tribble(
  ~building_name, ~year_inuse,
        "office",       2020,
        "office",       2021,
        "office",       2022,
        "office",       2023,
         "house",       2020,
         "house",       2021,
         "house",       2022,
         "house",       2023,
        "retail",       2020,
        "retail",       2021,
        "retail",       2022,
        "retail",       2023
  )

data %>% 
  group_by(building_name) %>% 
  mutate(first_year = if_else(year_inuse == min(year_inuse), 'yes', 'no')) %>% 
  ungroup()

给出

# A tibble: 12 x 3
   building_name year_inuse first_year
   <chr>              <dbl> <chr>     
 1 office              2020 yes       
 2 office              2021 no        
 3 office              2022 no        
 4 office              2023 no        
 5 house               2020 yes       
 6 house               2021 no        
 7 house               2022 no        
 8 house               2023 no        
 9 retail              2020 yes       
10 retail              2021 no        
11 retail              2022 no        
12 retail              2023 no