vicky
vicky

Reputation: 9

Finding the highest sum of two columns in a data set

I have the following 3 column data set in R:

 Year  Boys  Girls
 2018  5000  4000
 2019  5030  4050
 2020  5040  4010

How would I be able to find which Year had the highest total of Boys + Girls (using R)?

Nothing I have seen after searching online shows me the code that will show the year that had the highest total, as well as the total number.

Upvotes: 0

Views: 255

Answers (2)

Simon Bonner
Simon Bonner

Reputation: 101

And the tidyverse way

## Construct data frame
df <- tibble(Year = 2018:2020, 
Boys=c(5000,5030,5040), 
Girls=c(4000,4050,4010))

df %>% 
mutate(Total = Boys + Girls) %>% # Add totals to the data frame
filter(Total == max(Total))   # Extract row with highest total

Upvotes: 0

Benjamin Ye
Benjamin Ye

Reputation: 518

To show both the year and the highest total, use the following code:

df$Total <- df$Boys + df$Girls
df[which.max(df$Boys + df$Girls), c('Year', 'Total')]

The first line adds a column called Total with the total number of boys and girls, and the second line determines which row the largest total count is in and reports the value of the Year and the Total from that row.

Upvotes: 1

Related Questions