Home > Enterprise >  How do I get the value from 1 column based on highest value of another column of the same row?
How do I get the value from 1 column based on highest value of another column of the same row?

Time:01-19

Here's the data

# A tibble: 7 × 2
  DayOfWeek      n
      <int>  <int>
1         1 327828
2         2 355398
3         3 333699
4         4 284276
5         5 267085
6         6 321255
7         7 299569

What I hope to get is 2 . Which is the highest count. And if there is more than 1 for the highest count I'll get both values.

what I have so far:

library(data.table)
library(dplyr)

#import data
y00Delay <- fread("2000.csv.bz2",select = c("CRSDepTime","DayOfWeek","DayofMonth","Month","ArrDelay","DepDelay"))

#find all departure and arrival delay 
y00NoDelay <- filter(y00Delay, ArrDelay <=0 & DepDelay <=0)

#group by day of week then count the frequency of each day
y00NoDelay_DayOfWeek <- y00NoDelay %>%
  group_by(DayOfWeek) %>%
  summarise(n = n()) 

CodePudding user response:

Do you want to get this?

df %>% 
  mutate(max = DayOfWeek[which.max(n)])

  DayOfWeek      n max
1         1 327828   2
2         2 355398   2
3         3 333699   2
4         4 284276   2
5         5 267085   2
6         6 321255   2
7         7 299569   2
  •  Tags:  
  • Related