Home > Enterprise >  How to select the lowest number per group in R
How to select the lowest number per group in R

Time:07-07

rn=c(3,4,5,2,1,5,6,8,10,3,4,5,6,8,9,7)
na=c("A","A","A","A","A","B","B","B","B","B","CD","CD","CD","CD","CD","CD")
mo=c("ram","okd","mlu","lom","mpl","mpl","cdd","jjh","yyt","uu","tt","rre","llm","mm","mlp","lok")
dat=cbind(rn,na,mo)

I need to select for each group in na the lowest number in rn

example output:

    "1"  "A"  "mpl"
    "3"  "B"  "uu" 
    "4"  "CD" "tt" 

CodePudding user response:

We can use slice_min after grouping (assuming 'dat' is data.frame and not a matrix) - cbind by default returns a matrix, instead use data.frame directly

library(dplyr)
dat %>% 
  group_by(na) %>%
  slice_min(n = 1, order_by = rn) %>%
  ungroup

-output

# A tibble: 3 × 3
     rn na    mo   
  <dbl> <chr> <chr>
1     1 A     mpl  
2     3 B     uu   
3     4 CD    tt   

data

dat <- data.frame(rn, na, mo)

CodePudding user response:

base R option:

rn=c(3,4,5,2,1,5,6,8,10,3,4,5,6,8,9,7)
na=c("A","A","A","A","A","B","B","B","B","B","CD","CD","CD","CD","CD","CD")
mo=c("ram","okd","mlu","lom","mpl","mpl","cdd","jjh","yyt","uu","tt","rre","llm","mm","mlp","lok")
dat=data.frame(rn,na,mo)

dat[dat$rn==ave(dat$rn, dat$na, FUN=min),]
#>    rn na  mo
#> 5   1  A mpl
#> 10  3  B  uu
#> 11  4 CD  tt

Created on 2022-07-06 by the reprex package (v2.0.1)

CodePudding user response:

Base R solution:

do.call(rbind,lapply(split.data.frame(dat, dat$na), function(x) x[which.min(x$rn),]))
   rn na  mo
A   1  A mpl
B   3  B  uu
CD  4 CD  tt

CodePudding user response:

We can do this even with a simple filter after creating the data.frame

Data

dat=data.frame(rn,na,mo)

Procedure

library(dplyr)

dat %>%
  group_by(na) %>%
  slice_min(n = 1, order_by = rn) %>%
  ungroup()

Output

# A tibble: 3 x 3
     rn na    mo   
  <dbl> <chr> <chr>
1     1 A     mpl  
2     3 B     uu   
3     4 CD    tt 
  •  Tags:  
  • r
  • Related