返回一组列中的第二大值

时间:2017-02-06 16:24:09

标签: r dplyr

我有以下数据框:

dff <- structure(list(`MCI ID` = c("070405344", "230349820", "260386435","370390587", "380406805", "391169282", "440377986", "750391394","890373764", "910367024"), `123a_1` = structure(c(16672, 16372,16730, 16688, 16700, 16783, 16709, 17033, 16786, 16675), class = "Date"),`123a_2` = structure(c(17029, 16422, 17088, 17036, 17057,17140, 17072, 17043, 17141, 17038), class = "Date"), `123a_3` = structure(c(NA_real_,NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,NA_real_, NA_real_, NA_real_), class = "Date"), `123a_4` = structure(c(NA_real_,NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,NA_real_, NA_real_, NA_real_), class = "Date"), `123a_5` = structure(c(NA_real_,NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,NA_real_, NA_real_, NA_real_), class = "Date"), max123a = structure(c(17029,16422, 17088, 17036, 17057, 17140, 17072, 17043, 17141, 17038), class = "Date")), .Names = c("MCI ID", "123a_1", "123a_2","123a_3", "123a_4", "123a_5", "max123a"), row.nam... <truncated>

我已经为123a_1123a_5的每一行中的最大一列提供了一列。为此,我能够使用:

dff <- mutate(dff, max123a = pmax(`123a_1`, `123a_2`, `123a_3`, `123a_4`, `123a_5`, na.rm = T))

然而,现在我需要每行的第二大。这假定NA123a_3中可能存在123a_5以外的数据。理想情况下,我喜欢dplyr解决方案,因此我可以将两个命令组合在一起,但我会采取任何措施。

2 个答案:

答案 0 :(得分:2)

dplyrtidyr

library(dplyr)
library(tidyr)
dff %>% 
  gather(var, val, 2:6) %>% 
  group_by(`MCI ID`) %>% 
  summarise(max2 = max(val[val != max(val, na.rm = TRUE)], na.rm = TRUE)) %>% 
  left_join(dff, .)

这导致:

      MCI ID     123a_1     123a_2 123a_3 123a_4 123a_5    max123a       max2
1  070405344 2015-08-25 2016-08-16   <NA>   <NA>   <NA> 2016-08-16 2015-08-25
2  230349820 2014-10-29 2014-12-18   <NA>   <NA>   <NA> 2014-12-18 2014-10-29
3  260386435 2015-10-22 2016-10-14   <NA>   <NA>   <NA> 2016-10-14 2015-10-22
4  370390587 2015-09-10 2016-08-23   <NA>   <NA>   <NA> 2016-08-23 2015-09-10
5  380406805 2015-09-22 2016-09-13   <NA>   <NA>   <NA> 2016-09-13 2015-09-22
6  391169282 2015-12-14 2016-12-05   <NA>   <NA>   <NA> 2016-12-05 2015-12-14
7  440377986 2015-10-01 2016-09-28   <NA>   <NA>   <NA> 2016-09-28 2015-10-01
8  750391394 2016-08-20 2016-08-30   <NA>   <NA>   <NA> 2016-08-30 2016-08-20
9  890373764 2015-12-17 2016-12-06   <NA>   <NA>   <NA> 2016-12-06 2015-12-17
10 910367024 2015-08-28 2016-08-25   <NA>   <NA>   <NA> 2016-08-25 2015-08-28

你可以按如下方式一起做所有事情:

dff %>% 
  gather(var, val, 2:6) %>% 
  group_by(`MCI ID`) %>% 
  summarise(max2 = max(val[val != max(val, na.rm = TRUE)], na.rm = TRUE)) %>% 
  left_join(dff,.) %>% 
  mutate(max123a = pmax(`123a_1`, `123a_2`, `123a_3`, `123a_4`, `123a_5`, na.rm = TRUE))

基础R的解决方案:

dff$max2 <- apply(dff[2:6], 1, function(x) rev(sort(x))[2])

答案 1 :(得分:1)

我们可以使用tidyverse

library(tidyverse)
dff %>%
      summarise_each(funs(rev(sort(.))[2]))