R语言 如果行在某些列中具有相同的值,则减少行[重复]

zf2sa74q  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(114)

此问题已在此处有答案

How to use dplyr's coalesce function with group_by() to create one row per person with all values filled in?(2个答案)
Handling NA values in apply and unique(2个答案)
22小时前关闭
我正在使用r,我遇到的问题是,我希望每个id和date都有唯一的time 1和time 2值,但对于某些行,它们是分开的。我想通过组合列的值来合并组合某些列中具有相同值的行。
具体来说,如果“id”和“date”匹配,我想将两行减少为一行。

  1. id <- c('A', 'B', 'C', 'B', 'C'); date <- c('2023-10-09', "2023-10-10", "2023-10-10", "2023-10-10", "2023-10-10")
  2. time1 <- c('2023-10-09 05:55:55', '2023-10-10 05:55:55', NA, NA, '2023-10-10 10:55:55')
  3. time2 <- c('2023-10-09 06:10:55', NA, '2023-10-10 20:55:55', '2023-10-10 20:59:55', NA)
  4. df <- data.frame(id, date, time1, time2)
  5. > df
  6. id date time1 time2
  7. 1 A 2023-10-09 2023-10-09 05:55:55 2023-10-09 06:10:55
  8. 2 B 2023-10-10 2023-10-10 05:55:55 <NA>
  9. 3 C 2023-10-10 <NA> 2023-10-10 20:55:55
  10. 4 B 2023-10-10 <NA> 2023-10-10 20:59:55

我想要的是

  1. id <- c('A', 'B', 'C'); date <- c('2023-10-09', "2023-10-10", "2023-10-10")
  2. time1 <- c('2023-10-09 05:55:55', '2023-10-10 05:55:55','2023-10-10 10:55:55')
  3. time2 <- c('2023-10-09 06:10:55', '2023-10-10 20:55:55', '2023-10-10 20:59:55')
  4. df <- data.frame(id, date, time1, time2)
  5. > df
  6. id date time1 time2
  7. 1 A 2023-10-09 2023-10-09 05:55:55 2023-10-09 06:10:55
  8. 2 B 2023-10-10 2023-10-10 05:55:55 2023-10-10 20:55:55
  9. 3 C 2023-10-10 2023-10-10 10:55:55 2023-10-10 20:59:55

任何输入都将有帮助!

7jmck4yq

7jmck4yq1#

尝试

  1. library(dplyr)
  2. df |>
  3. mutate(time1 = min(time1, na.rm = TRUE),
  4. time2 = max(time2, na.rm = TRUE),
  5. .by = c(id, date)) |>
  6. unique()
  7. #> id date time1 time2
  8. #> 1 A 2023-10-09 2023-10-09 05:55:55 2023-10-09 06:10:55
  9. #> 2 B 2023-10-10 2023-10-10 05:55:55 2023-10-10 20:59:55
  10. #> 3 C 2023-10-10 2023-10-10 10:55:55 2023-10-10 20:55:55

创建于2023-10-11使用reprex v2.0.2

相关问题