Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()) 

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading