Automatic groups with groupdata2

  collapse = TRUE,
  comment = "#>",
  dpi = 92,
  fig.retina = 2
options(tibble.print_min = 4L, tibble.print_max = 4L)


In this vignette, we will use the 'l_starts' method with group() to allow transferring of information from one dataset to another. We will use the automatic grouping function that finds group starts all by itself.

Attach packages

library(dplyr) # %>%
library(knitr) # kable


3 participants were asked to solve a task. They had to take turns but could go for multiple runs of the task before taking a break and letting the next participant take over. They had 2 turns each. Let's call each turn a session, i.e. there was 6 sessions. A team of experts would rate how well the participant did throughout the entire session, meaning that if the participant had some bad runs, they would have to make a choice whether to save energy for the other session or whether to try and correct the rating of the current session. For each run of the task, we recorded how many errors the participant made.

df_observations <- data.frame(
  "run" = 1:30,
  "participant" = c(1,1,1,1,
  "errors" = c(3,2,5,3,

# Show the first 20 rows of data frame
df_observations %>% head(20) %>%  kable()

df_ratings <- data.frame(
  "session" = c(1:6),
  "rating" = c(3,8,2,5,9,4)

df_ratings %>% kable()

We would like to get the ratings into the data frame with observations. For this we will first create a session column, and then get the ratings for the sessions. To do this we will use group() with the 'l_starts' method. This methods takes group start values, finds those values in a specified column, and creates groups that begin at the start values. To show this, let's try it out with some start values before having group() find them automatically.

group(df_observations, n = c(1,2,3,1,2,3), method = 'l_starts', 
      starts_col = 'participant', col_name = 'session') %>% 

group() went through the participant column and found one value from n at a time. When it encountered the value, it noted down the row index and continued down the column searching for the next value in n. In the end it started groups at the found row indices from top to bottom. Since our data has the same value in the participant column for the entire session, we can actually get group() to find these group starts automatically. It will go through the given column, and whenever it encounters a new value, i.e. one that is different from the previous row, it starts a new group.

df_observations <- group(df_observations, n = 'auto', 
                         method = 'l_starts',
                         starts_col = 'participant', 
                         col_name = 'session') 

df_observations %>% 

And it works! :)
If you just want the group starts, you can use the function find_starts().

Now that we have the session information, we can transfer the ratings from the ratings data frame.

df_merged <- merge(df_observations, df_ratings, by = 'session')

# Show head of df_merged
df_merged %>% head(15) %>% kable()

Now, we can find the average number of errors per session and see if they correlate with the experts' ratings.

avg_errors <- df_merged %>% 
  group_by(session) %>% 
  dplyr::summarize("avg_errors" = mean(errors))

avg_errors %>% kable()

Let's transfer the averages to the merged data frame. Once again, we just use merge(). Since we have just one rating per session, we will get only the first row of each session.

df_summarized <- merge(df_merged, avg_errors, by = 'session') %>% 
  group_by(session) %>%  # For each session
  filter(row_number()==1) %>%  # Get first row
  select(-errors) # Remove errors column as we use avg_errors now

df_summarized %>% kable()

We have 1 row per session with the participant, the rating and the average errors. If we wanted to know how many runs a session contained, we could extract it from the 'run' column.

Now let's check if there's a correlation between ratings and average errors.

cor(df_summarized$rating, df_summarized$avg_errors)

It seems they are highly negatively correlated, so participants with fewer errors have higher ratings and vice versa.


Well done, you made it to the end of this introduction to groupdata2! If you want to know more about the various methods and arguments, you can read the Description of groupdata2.
If you have any questions or comments to this vignette (tutorial) or groupdata2, please send them to me at, or open an issue on the github page so I can make improvements.


Try the groupdata2 package in your browser

Any scripts or data that you put into this service are public.

groupdata2 documentation built on March 7, 2021, 1:07 a.m.