在R中,寻找更好的方法从子组中获取最大值(组内的组)

时间:2017-04-16 08:45:33

标签: r greatest-n-per-group tidyr

对于包含X,Y,Z组的给定DF,如果在该列中存在相应的PI值,我想在多个列(A_Column,C_Column,N_Column)中的每一列中保留包含最大值的行。行。例如,对于组X,应为各个列的PI值C和A保留最大值。以下是我的尝试 - 有更短/更优雅的方式来达到相同的效果吗?

我的首发DF:

> DF
  Group           H PI PC A_Column C_Column N_Column
1     X       AA001  C  C        2    -0.10       22
2     X         A16  A  C        3    -0.12       13
3     X    A2015_01  C  C        5    -0.80       51
4     X         AA1  C  A        2    -0.30       32
5     Y   AAA-16-04  A  A        5    -0.20       15
6     Y    A01_2009  O  A        8    -0.40       28
7     Z        AA02  A  A       17    -0.30       12
8     Z AAD003-2014  A  N        3    -0.13       43
9     Z         AD4  N  N        5    -0.60       45

输出我最终:

> DF_max
  Group         H PI PC A_Column C_Column N_Column
1     X     AA001  C  C        2    -0.10       22
2     X       A16  A  C        3    -0.12       13
5     Y AAA-16-04  A  A        5    -0.20       15
7     Z      AA02  A  A       17    -0.30       12
9     Z       AD4  N  N        5    -0.60       45

我的代码:

library(dplyr)
library(tidyverse)

# toy example to get the maximum values out of every group

Group <- c("X","X","X","X","Y","Y","Z","Z","Z")
H <-c("AA001","A16","A2015_01","AA1","AAA-16-04","A01_2009","AA02","AAD003-2014","AD4")
PI <- c("C","A","C","C","A","O","A","A","N")
PC <- c("C","C","C","A","A","A","A","N","N")
A_Column <- c(2,3,5,2,5,8,17,3,5)
C_Column <- -c(.1,.12,.8,.3,.2,.4,.3,.13,.6)
N_Column <- c(22,13,51,32,15,28,12,43,45)

DF <- data.frame(Group, H, PI, PC, A_Column, C_Column, N_Column)
DF

# tidy data column Values-Labels before using dplyr
gather_DF <- gather(DF, key = Col_labels, value = Obs, -Group, -H, -PI, -PC)
gather_DF

# look for value label matches within each group
gather_DF$Col_labels_match <- gather_DF$Col_labels 
map = setNames(c("A", "C", "N"), c("A_Column", "C_Column", "N_Column"))
gather_DF$Col_labels_match <- map[unlist(gather_DF$Col_labels_match)]

# get max values per group, where PI equals Col_labels
max_DF <- gather_DF %>% filter(Col_labels_match==PI) %>% group_by(Group, PI) %>% top_n(1, Obs)
max_ID <- unique(max_DF$H)
DF_max <- DF[which(DF$H %in% max_ID),] # pull max values out of DF to get original formatting
DF_max

更新: 下面的代码使用@ Arun的data.table方法 - 但是如果PI没有放在引号中,则会出现错误(参见下文;我使用“PI”而不是PI进行了更正)并且输出组gY没有正确的最大值( G = 6而不是G = 17)。

df <- structure(list(Group = c("gX", "gX", "gY", "gY", "gY", "gZ", 
                               "gW", "gW", "gV", "gV", "gV", "gT", "gR", "gR", "gR", "gR", "gR", 
                               "gS", "gQ", "gL"), PI = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 
                                                                   1L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 1L), .Label = c("C", 
                                                                                                                                   "G", "O"), class = "factor"), H = 1:20, PC = structure(c(2L, 
                                                                                                                                                                                            2L, 2L, 2L, 2L, 2L, 1L, 1L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 
                                                                                                                                                                                            2L, 2L, 1L), .Label = c("C", "G", "I", "O"), class = "factor"), 
                     C = c(NA, NA, NA, NA, NA, NA, 3, 1, NA, NA, NA, NA, NA, NA, 
                           NA, NA, NA, NA, NA, 2), I = c(NA, NA, NA, NA, NA, NA, NA, 
                                                         NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1), G = c(16, 
                                                                                                                   10, 6, 17, 12, 14, 13, 11, NA, NA, NA, 9, 5, 2, 15, 3, 1, 
                                                                                                                   7, 8, 4), N = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                   NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                   NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                   NA_real_, NA_real_, NA_real_), O = c(NA, NA, NA, NA, NA, 
                                                                                                                                                                        NA, NA, NA, 3, 2, 1, NA, 8, 4, 5, 7, 6, NA, NA, NA)), .Names = c("Group", 
                                                                                                                                                                                                                                         "PI", "H", "PC", "C", "I", "G", "N", "O"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                  -20L), class = "data.frame")
df

dt <- data.table(Group=df$Group, PI=df$PI, H=df$H, PC=df$PC, 
                 C = df$C, 
                 I = df$I, 
                 G = df$G, 
                 N = df$N, 
                 O = df$O)


> Maxdt <- dt[PI %in% c("C", "I", "G", "N", "O"),
> +             .SD[which.max(get(PI))],
> +             by=.(Group, PI)]  
Error in get(PI) : invalid first argument

Maxdt <- dt[PI %in% c("C", "I", "G", "N", "O"),
                         .SD[which.max(get("PI"))],
                         by=.(Group, PI)] 



    > Maxdt
    Group PI  H PC  C  I  G  N  O
 1:    gX  G  1  G NA NA 16 NA NA
 2:    gY  G  3  G NA NA  6 NA NA
 3:    gZ  G  6  G NA NA 14 NA NA
 4:    gW  C  7  C  3 NA 13 NA NA
 5:    gV  O  9  O NA NA NA NA  3
 6:    gT  G 12  I NA NA  9 NA NA
 7:    gR  O 13  O NA NA  5 NA  8
 8:    gS  G 18  G NA NA  7 NA NA
 9:    gQ  G 19  G NA NA  8 NA NA
10:    gL  C 20  C  2  1  4 NA NA

3 个答案:

答案 0 :(得分:3)

这是使用 data.table 的另一种方式:

dt <- data.table(Group, H, PI, PC, A=A_Column, C=C_Column, N=N_Column)
dt[PI %in% c("A", "C", "N"),
    .SD[which.max(get(PI))],
    by=.(Group, PI)]        

#    Group PI         H PC  A     C  N
# 1:     X  C     AA001  C  2 -0.10 22
# 2:     X  A       A16  C  3 -0.12 13
# 3:     Y  A AAA-16-04  A  5 -0.20 15
# 4:     Z  A      AA02  A 17 -0.30 12
# 5:     Z  N       AD4  N  5 -0.60 45

跟踪正在发生的事情应该非常简单。在PIA,C,N匹配的任何行中,我们按Group, PI进行分组.. .SD包含 D <的 S ubset / strong>每个组的ata ..

get(PI)返回与PI中存储的字符值对应的值,并为每个PI返回Group, PI指向的该列的最大值对应的行}。

例如,对于第一个组合,Group=X, PI=Cget(PI) == get("C"),为c(-0.1, -0.8, -0.3)返回索引1的组返回which.max

答案 1 :(得分:0)

你在找这样的东西吗?

&#xA;&#xA;
  DF%&gt;%&#xA; group_by(Group,PI)%&gt;%&#xA; mutate(sel =(A_Column == max(A_Column))+(C_Column == max(C_Column))+(N_Column == max(N_Column)))%&gt;%&#xA; filter(sel == max(sel))%&gt;%&#xA; select(-sel)&#xA;  
&#xA;&#xA;

上述代码的结果是:

&#xA;&#xA;
  #Source:本地数据框[6 x 7]&#xA; #Groups:Group,PI [6]&#xA;#&#xA; #Group H PI PC A_Column C_Column N_Column&#xA;#&lt ; FCTR&GT; &LT; FCTR&GT; &LT; FCTR&GT; &LT; FCTR&GT; &LT; DBL&GT; &LT; DBL&GT; &lt; dbl&gt;&#xA;#1 X A16 AC 3 -0.12 13&#xA;#2 X A2015_01 CC 5 -0.80 51&#xA;#3 Y AAA-16-04 AA 5 -0.20 15&#xA;#4 Y A01_2009 OA 8 -0.40 28&#xA;#5 Z AAD003-2014 AN 3 -0.13 43&#xA;#6 Z AD4 NN 5 -0.60 45&#xA;  
&#xA;

答案 2 :(得分:0)

不确定这是更短还是更优雅,但data.table这样做的方法是:

# Convert to data.table, rename columns and convert PI from factor to character
library(data.table)
setDT(DF)
setnames(DF, c("A_Column", "C_Column", "N_Column"), c("A", "C", "N"))
DF[, PI := as.character(PI)]

# Melt A, C and N columns to allow filtering (variable == PI) when finding
# max value by group and PI
dt_melt <- melt(DF, id.vars = c("Group", "H", "PI", "PC"))
dt_max <- dt_melt[variable == PI, .(value = max(value)),
  by = .(Group, PI, variable)]

# Use Group, PI and max values as index to pull original rows from melt, and
# unique to get rid of ties
dt_idx <- unique(dt_melt[dt_max[, .(Group, PI, variable, value)],
  on = c("Group", "PI", "variable", "value")])

# Use Group, H, PI, and PC to pull original rows from DF
out <- DF[dt_idx[, .(Group, H, PI, PC)], on = c("Group", "H", "PI", "PC")]
setnames(out, c("A", "C", "N"), c("A_Column", "C_Column", "N_Column"))

# > out
#    Group         H PI PC A_Column C_Column N_Column
# 1:     X       A16  A  C        3    -0.12       13
# 2:     Y AAA-16-04  A  A        5    -0.20       15
# 3:     Z      AA02  A  A       17    -0.30       12
# 4:     X     AA001  C  C        2    -0.10       22
# 5:     Z       AD4  N  N        5    -0.60       45