简体   繁体   中英

Extract information from a row, based on two different columns

I have a dataset that looks something like this:

ID           Name            Age           Medal         Country
1            Dimitrios       10            Bronze        GRE
2            Oumar           30            NA            SEN
3            Toivo           25            Gold          FIN

What I would like to do is extract the information (name + age + country) of the youngest athlete that has won a medal (Bronze, Silver and Gold).

Is this possible? If so, could you please help me as to how I should do just that.

To get the youngest athlete who won medal X for X in {bronze, silver, gold}, here's one way using dplyr:: .

library(dplyr)

# make some fake data 
dat <- data.frame(
  ID = 1:7, 
  Name = c("a","b","c","d","e","f","g"),
  Age = c(10, 15, 20, 25, 30, 35, 40), 
  Medal = c("bronze","bronze","silver","silver","gold","gold",NA),
  Country = c("GRE","SEN","FIN","USA","GRE","USA","FIN"))

# get just the rows where the person is the youngest to win their medal type:
dat %>% group_by(Medal) %>% 
  filter(Age == min(Age)) %>% ungroup()

## output will look like this:
## 
##    ID Name   Age  Medal   Country
##     1  a      10  bronze  GRE    
##     3  c      20  silver  FIN    
##     5  e      30  gold    GRE    
##     7  g      40  NA      FIN    

This will also return the youngest person with NA as their value in $Medal . To just get the info for the three medal winners and ignore NA s, just filter() the data first:

dat %>% 
  filter(!is.na(Medal)) %>% 
  group_by(Medal) %>% 
  filter(Age == min(Age)) %>% ungroup()

Or if you just want to get the info of the youngest person to ever win any medal, then you can just filter the data directly (ie without grouping by $Medal first):

dat %>% 
  filter(!is.na(Medal)) %>% 
  filter(Age == min(Age))

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