简体   繁体   中英

Create indicator for two highest observations?

I am working with the following data frame:

Month      Week   Y     Name   Color
January     2     1.2    Joe    Red
January     2     3.3    Eric   Red
January     2     4.5    Mike   Blue
January     2     1.7    Brian  Blue
January     2     2.9    Pete   Red
January     3     4.6    Joe    Red
January     3     5.1    Eric   Blue
January     3     2.1    Mike   Blue
January     3     6.9    Pete   Red
...

I would like to create a new column ('Highest') which identifies the individuals with the two highest Y values in a given week (identifying them with A and B so it will be easier to create line segments later in my project) who also have the color 'Blue'.

Month      Week   Y     Name    Highest
January     2     1.2    Joe       -
January     2     3.3    Eric      B
January     2     4.5    Mike      A
January     2     1.7    Brian     -
January     2     2.9    Pete      -
January     3     4.6    Joe       -
January     3     5.1    Eric      B
January     3     2.1    Mike      A
January     3     6.9    Pete      -
...

Additionally, as you can see in the table above, I want the 'Highest' column to be the same throughout the entire month--the column should show individuals with the highest two Y values in week 2 for all observations in a given month. I'm assuming this will require group_by(Month, Week) %>%

You can arrange the data by Y values and assign 'A' , 'B' to first two values.

library(dplyr)

df %>%
  arrange(Month, Week, desc(Y)) %>%
  group_by(Month, Week) %>%
  mutate(Highest = c('A', 'B', rep(NA, n()-2)))
  #If you want to have '-' instead of `NA`. 
  #mutate(Highest = c('A', 'B', rep('-', n()-2)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM