簡體   English   中英

data.table 以另一列的結果為條件獲取列中的值

[英]data.table get the values in a column conditional to the results of another column

df = data.table(
 ID = c("A","B","C","D","E","F","G"),
 price = c(100,101,102,103,104,102,101),
 ID2=c("a","b","b","b","c","c","c"))

df
#ID price ID2
#1:  A   100   a
#2:  B   101   b
#3:  C   102   b
#4:  D   103   b
#5:  E   104   c
#6:  F   102   c
#7:  G   101   c

鑒於上面的例子,我想獲得以 ID2 分組的最高價格為條件的 ID。 我的 output 應該如下所示:

#   ID2   V1  ID
#1:   a  100   A
#2:   b  103   D
#3:   c  104   E

你可以做:

library(data.table)
df[, .SD[which.max(price)], by=ID2]

#   ID2 ID price
#1:   a  A   100
#2:   b  D   103
#3:   c  E   104

dplyr ,您將擁有:

library(dplyr)
df %>% 
  group_by(ID2) %>% 
  slice_max(price, n = 1) %>% 
  select(ID2, V1 = price, ID)

#  ID2      V1 ID   
#  <chr> <dbl> <chr>
#1 a       100 A    
#2 b       103 D    
#3 c       104 E    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM