简体   繁体   中英

How do I analyze data in a table with multiple subjects?

I'm trying to get started with R, but are having some issues. I've got data from a fake race I've invented. It had 4 drivers, where each driver did 3 laps each. The data I've got contains which driver it is, which lap it is, and how quickly he raced that lap. It looks something like this:

Driver Lap Time
d1     l1  42
d1     l2  38
d1     l3  37
d2     l1  40
d2     l2  39
etc...

My problem lies in finding a way to create an analysis that checks which lap the drivers did best on.

My result should say which lap for each individual driver was quickest, and which lap was quickest on average of all drivers. I have a rough understanding of how I would do this with only one driver, but I've got no clue as to how I can do it with multiple entities. I've tried running a Chi-square test, but that doesn't really work with respect to multiple drivers.

library("tidyverse")


# First, I'll generate a similar dataset.
data <- tibble(
               driver = rep(1:3, each=3),
               lap = rep(1:3, times=3),
               # Generate random integers between 30 and 50 for example time values.
               time = sample(30:50, 9))


# It sounds like there are two questions here:


# Question 1) For each driver, on what lap do they have the lowest time?

min_lap_per_driver <- data %>%
  # Group by driver, because we want to see a value per driver.
  group_by(driver) %>%
  # Then, filter for the row with the minimum time per lap.
  filter(time == min(time))
  # The resulting tibble now describes the lowest lap time for each driver.


# Question 2) What's the average time per lap? Which average is the lowest?

avg_time_per_lap <- data %>%
  # Group by lap (to see on a "per lap" basis)
  group_by(lap) %>%
  # Calculate the average of times per lap.
  summarise(avg_time = mean(time))

lap_with_lowest_time <- avg_time_per_lap %>%
  # Filter for the row with the minimum value of avg_time.
  filter(avg_time == min(avg_time))
  # The resulting tibble describes the lap with the lowest average time.

To learn more about creating summary tables, check out this section of R for Data Science . :)

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