Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - which and which.max fusion

Tags:

r

I have a simple question, how could I use which and which.max at the same time.

I would like to select the maximum epnum for the row id == B13639J2. I need to retreive the row number because I need to make some manual changes to the variable.

So max epnum of row id == 'B13639J2'

           id   epnum start
95528 B13639J2     1     0
95529 B13639J2     2   860
95530 B13639J2     3  1110
95531 B13639J2     4  1155
95532 B13639J2     5  1440

I was wondering how I could simply do something like

dta[which(dta$id == 'B13639J2' & which.max(dta$epnum)), ] 

Finally then, I need to delete the spotted row.

Thanks.

The data

dta = structure(list(id = c("B13639J1", "B13639J1", "B13639J1", "B13639J1", 
"B13639J1", "B13639J1", "B13639J1", "B13639J1", "B13639J2", "B13639J2", 
"B13639J2", "B13639J2", "B13639J2"), epnum = c(4, 5, 6, 7, 8, 
9, 10, 11, 1, 2, 3, 4, 5), start = c(420, 425, 435, 540, 570, 
1000, 1310, 1325, 0, 860, 1110, 1155, 1440)), .Names = c("id", 
"epnum", "start"), row.names = 95520:95532, class = "data.frame")
like image 906
giac Avatar asked Dec 24 '22 16:12

giac


1 Answers

One option if we are using numeric index (which/which.max) will be slice from dplyr. Here a double slice is needed. We first subset the 'id' i.e. 'B13639J2' and then subset again for the max of 'epnum' values.

 library(dplyr)
 slice(dta, which(id=='B13639J2')) %>%
                   slice(which.max(epnum))
 #        id epnum start
 #1 B13639J2     5  1440

Or we group by 'id', arrange the 'epnum' in descending order, and filter the first row with the specified 'id'.

  dta1 <- dta %>% 
             group_by(id) %>% 
             arrange(desc(epnum)) %>%
             filter(id=='B13639J2', row_number()==1L)

If we then want to remove this row from the dataset, one option is anti_join with the original dataset.

  anti_join(dta, dta1)

Or by changing the filter option this can be done

  dta %>%
      group_by(id) %>% 
      arrange(desc(epnum)) %>%
      filter(!(id=='B13639J2' & row_number()==1L))
like image 125
akrun Avatar answered Jan 16 '23 01:01

akrun