使用dplyr从另一列返回值

时间:2018-02-07 03:44:52

标签: r dplyr data.table

我正在尝试根据对另一列分组的同一行中不同列的过滤,从另一列返回值。

我试过了:

library(dplyr)
library(data.table)

column1<- c("A","B","C","A","B","C")
column2<- c(1,2,3,5,6,8)
column3<- c("Hat","Shirt","Pants","Shoes","Jacket","Coat")

Df<- data.table(column1,column2,column3)

Df<- Df %>%
  grouped_by(column1) %>%
  mutate(dfcol3filtered= Df$col3[Df$col2= min(df$col2)]) 

无济于事。

我想让dfcol3filtered返回每个分组的最小值。

这可以在excel中完成,但无法弄清楚如何在r。

中完成

1 个答案:

答案 0 :(得分:3)

解决方案。

Df %>%
  group_by(column1) %>%
  mutate(dfcol3filtered = column3[which.min(column2)]) %>%
  ungroup()
# # A tibble: 6 x 4
#   column1 column2 column3 dfcol3filtered
#   <chr>     <dbl> <chr>   <chr>         
# 1 A          1.00 Hat     Hat           
# 2 B          2.00 Shirt   Shirt         
# 3 C          3.00 Pants   Pants         
# 4 A          5.00 Shoes   Hat           
# 5 B          6.00 Jacket  Shirt         
# 6 C          8.00 Coat    Pants 

解决方案。

Df[, dfcol3filtered := column3[which.min(column2)], by = column1][]
#    column1 column2 column3 dfcol3filtered
# 1:       A       1     Hat            Hat
# 2:       B       2   Shirt          Shirt
# 3:       C       3   Pants          Pants
# 4:       A       5   Shoes            Hat
# 5:       B       6  Jacket          Shirt
# 6:       C       8    Coat          Pants

基于加入的另一个解决方案。

Df %>%
  group_by(column1) %>%
  filter(column2 == min(column2)) %>%
  select(column1, dfcol3filtered = column3) %>%
  right_join(Df, by = "column1") %>%
  select(names(Df), dfcol3filtered) %>%
  ungroup()
# # A tibble: 6 x 4
#   column1 column2 column3 dfcol3filtered
#   <chr>     <dbl> <chr>   <chr>         
# 1 A          1.00 Hat     Hat           
# 2 B          2.00 Shirt   Shirt         
# 3 C          3.00 Pants   Pants         
# 4 A          5.00 Shoes   Hat           
# 5 B          6.00 Jacket  Shirt         
# 6 C          8.00 Coat    Pants