Data Wrangling

Getting Data into Useable Formats
Author

W. Joel Schneider

Published

March 1, 2024

Getting Used to Working with Data in R

In programs like SPSS, the data seem to live in a spreadsheet that is always open and available to you. In R, you can see the data whenever you want, but usually it sits unseen inside a variable. It can be disconcerting at first, but think of your data as living in a file somewhere on your hard drive (or online!), and then the data just come for a short visit in R.

The major benefit of working with data in R is that all of the changes, transformations, and restructuring happens in code—which can be recreated at any time. There is usually no need to “save” the data after you have transformed it. The next time you work with the data, you just run your code and all the calculations will transform the data exactly the same way as before.

Why does this matter? If you feel the need to save your data all the time, you end of having multiple copies of it: data.sav, data_restructured.sav, data_new.sav, data_new_final.sav, fixed_data_new_final.sav, restructued_final_with_missing_cases_removed.sav, and so forth and so on. It can be hard to figure out where to start the next time you work with your data. You might not remember which version has errors and which version has what you need.

In general, start with a completely raw data file (one that is exactly the way you started). Resist the temptation to make any changes to it directly. If you must, save a pristine copy somewhere and only then change it. Import your data and make all changes to it with code. One benefit of doing so is that the code documents any changes you would otherwise need to record in your lab notebook.

All the code in one place:

I am going to walk through these steps. You can run this all at once to make sure it works. Then I will explain it step by step.

# Load packages
library(tidyverse)

# Import data
d <- read_csv("https://github.com/wjschne/EDUC5529/raw/master/on_task_multiple_baseline.csv")

# View data
d
glimpse(d)
View(d)

# Filtering

## Cases with fewer than 8 baseline sessions
d %>% 
  filter(baseline_sessions_n < 8)  %>% 
  arrange(-baseline_sessions_n)


## Students with `id` equal to 7
d %>% 
  filter(id == 7)

## Students with scores above 10 on `time_1` AND `time_2`:
d %>% 
  filter(time_1 > 10 & time_2 > 10)

## Students with scores above 10 on `time_1` OR `time_2`:
d %>% 
  filter(time_1 > 10 | time_2 > 10)

# Selecting columns
## Select the id and time_1 variables
d %>% 
  select(id, time_1)

## Select 5 adjacent variables: time_1 throught time_5
d %>% 
  select(time_1:time_5)

## Remove the id variable
d %>% 
  select(-id)

## Select all variables that start with "time" 
d %>% 
  select(starts_with("time"))

# Select all variables end with 0:

d %>% 
  select(ends_with("0"))

# Select all variables that contain "_2"
d %>% 
  select(contains("_2"))

# Renaming variables
## Rename id to become student_id, rename baseline_sessions_n to baseline
d_new <- d %>% 
  rename(student_id = id, 
         baseline = baseline_sessions_n) 

## Rename rename all variables that start with "time_" 
## so that they start with "T_" instead
d %>% 
  rename_with(str_replace, pattern = "time_", replace = "T_")

# Make small data
# Select variables from id to time_3 and filter rows so that 
# id is less than 3 (not inclusive)
d_small <- d %>% 
  select(id:time_3) %>% 
  filter(id < 3)

d_small

# Restructuring from wide to long format:
d_small_longer <- d_small %>%
  pivot_longer(cols = time_1:time_3,
               names_to = "time",
               values_to = "on_task",
               names_prefix = "time_", 
               names_transform = list(time = as.integer)) 

# Restructing from long to wide format:
d_small_longer %>% 
  pivot_wider(names_from = time, 
              values_from = on_task, 
              names_prefix = "time_")


# Restructure whole data from wide to long and create new variables
d_longer <- d %>%
  pivot_longer(cols = time_1:time_60,
               names_to = "time",
               values_to = "on_task",
               names_prefix = "time_", 
               names_transform = list(time = as.integer)) %>% 
    mutate(phase = case_when(
    time <= baseline_sessions_n ~ "Pre-Intervention",
    time <= baseline_sessions_n + intervention_sessions_n ~ "Intervention",
    time > baseline_sessions_n + intervention_sessions_n ~ "Post-Intervention" 
  )) %>% 
  mutate(phase = fct_inorder(phase))

# Plot
d_longer %>% 
  mutate(id = factor(id) %>% fct_reorder(baseline_sessions_n)) %>% 
  ggplot(aes(time, on_task)) +
  geom_line(aes(group = id, color = phase)) + 
  facet_grid(rows = vars(id))

Importing Data from a file

For this task we will use a file that lives here on the web:

on_task_multiple_baseline.csv

You can download it if you want, but you do not need to. You can import it over the web right into R.

We have a .csv file, so we will use the tidyverse function read_csv, from the readr package, which can be loaded with the tidyverse package:

library(tidyverse)
d <- read_csv("https://github.com/wjschne/EDUC5529/raw/master/on_task_multiple_baseline.csv")

Note that we have to assign the data to a variable—d in this case. Otherwise, the data will spill into the console, but R will not remember the data.

Inspecting Data

The read_csv function imports a special data structure called a tibble which is a variation on the traditional data.frame structure. It has some useful defaults that make working with it more predictable and practical.

If the data is too big to print in your console, the tibble will just show the first few rows, the first few columns, and then the remaining column names. It all says what kind of data is in each column and how many rows and columns are in the data.

Whoa! That dataset has a lot of variables! When the there are more variables than rows, we say that the data is “wide.”

An alternate way to view the data is to use the glimpse function:

glimpse(d)

If you really need to see all the data, use the View function:

View(d)
d
id baseline_sessions_n intervention_sessions_n time_1 time_2 time_3 time_4 time_5 time_6 time_7 time_8 time_9 time_10 time_11 time_12 time_13 time_14 time_15 time_16 time_17 time_18 time_19 time_20 time_21 time_22 time_23 time_24 time_25 time_26 time_27 time_28 time_29 time_30 time_31 time_32 time_33 time_34 time_35 time_36 time_37 time_38 time_39 time_40 time_41 time_42 time_43 time_44 time_45 time_46 time_47 time_48 time_49 time_50 time_51 time_52 time_53 time_54 time_55 time_56 time_57 time_58 time_59 time_60
1 22 20 11.481 14.402 6.151 9.254 9.580 8.160 10.125 11.120 9.203 10.838 9.181 8.436 9.977 13.242 9.317 8.276 9.208 10.888 7.225 11.47 8.570 10.583 11.04 13.13 18.00 12.965 23.840 21.40 22.653 30.187 29.49 31.359 29.32 33.78 38.95 37.28 42.57 37.73 44.34 44.61 49.51 49.60 48.72 50.88 51.23 49.04 51.27 49.00 51.13 48.72 50.64 51.24 54.46 48.84 50.35 54.54 52.77 50.68 50.18 50.03
2 18 19 9.114 8.586 11.010 9.356 8.095 8.772 8.953 7.443 11.057 7.590 11.087 10.670 7.843 9.018 11.266 5.886 7.388 8.107 12.111 14.83 13.107 18.156 21.65 22.82 24.85 25.152 29.230 31.50 30.092 34.848 29.83 42.884 40.94 45.12 48.62 48.63 47.46 47.03 48.66 47.54 50.80 50.58 49.93 48.24 49.41 48.22 47.98 47.59 44.75 43.74 49.15 48.02 44.89 48.28 47.23 48.91 44.84 45.47 49.48 50.35
3 5 18 7.244 14.309 7.979 11.175 12.287 13.146 11.694 18.998 16.125 20.296 21.905 21.952 27.114 27.933 27.448 30.747 36.685 37.337 39.197 43.84 44.752 43.358 46.34 47.01 44.50 46.507 44.034 47.02 48.691 43.648 47.70 49.477 48.00 46.17 48.51 43.80 48.55 48.30 43.90 47.10 51.74 40.91 45.88 49.41 45.65 43.96 46.60 44.57 42.60 47.05 46.58 43.50 42.45 47.22 44.16 49.38 45.00 46.11 47.09 44.92
4 32 20 11.297 14.307 14.811 8.678 9.533 7.823 9.561 8.209 12.898 8.244 8.548 8.616 7.586 8.921 9.199 8.711 11.026 10.804 11.591 10.52 9.027 7.848 12.32 10.07 12.20 7.872 8.831 10.43 8.886 8.553 11.84 7.608 11.14 13.53 14.89 19.39 22.95 23.99 20.03 27.08 28.21 27.32 34.87 33.65 36.78 37.48 40.53 41.81 40.75 45.51 45.75 47.57 56.03 51.27 51.98 47.75 48.65 50.63 48.45 50.34
5 19 20 10.667 6.804 8.303 14.498 6.868 12.367 9.414 9.444 8.745 14.305 10.961 10.451 14.828 6.272 10.096 12.403 8.464 8.629 9.839 14.45 13.049 16.131 18.19 17.92 23.90 22.576 23.059 25.57 29.038 31.427 33.96 36.342 38.47 41.44 40.76 44.75 47.46 48.82 52.24 50.29 51.08 48.52 51.01 49.59 50.64 47.94 45.46 52.18 48.01 47.01 50.80 47.71 54.90 49.21 52.97 51.58 47.75 53.56 48.21 50.77
6 24 20 13.027 7.471 13.680 5.769 10.789 8.699 9.323 8.312 12.865 9.967 6.926 7.546 10.175 12.912 5.829 8.554 10.514 12.225 9.299 9.09 9.931 10.490 12.35 11.75 10.45 13.391 16.231 16.95 21.799 21.528 21.88 25.989 26.67 32.07 32.79 35.02 37.48 32.87 35.37 41.40 46.80 49.06 46.94 50.27 54.22 50.03 51.80 49.74 48.78 48.08 48.62 48.07 48.06 46.87 50.26 44.34 51.32 48.04 54.79 48.74
7 10 19 9.695 9.174 10.637 9.136 7.362 14.267 9.448 12.378 6.093 5.854 9.539 14.262 13.727 19.425 20.669 22.576 26.741 24.729 28.852 30.76 31.628 34.361 36.10 40.92 39.43 41.137 43.049 46.76 46.199 48.769 47.37 49.477 52.47 49.37 44.54 48.07 46.38 47.66 45.35 49.10 48.02 47.50 49.15 49.00 47.31 46.44 49.66 48.07 46.29 45.44 48.75 49.89 49.42 48.00 50.41 46.83 46.45 48.05 48.95 52.76
8 13 20 10.596 11.746 10.903 11.403 8.263 7.589 8.232 7.295 10.028 10.105 9.797 11.889 7.041 12.705 13.626 14.990 19.614 23.575 22.594 25.97 26.860 30.064 29.59 31.23 34.26 36.188 38.330 42.61 43.275 42.504 42.51 51.437 53.72 50.31 48.79 51.41 53.23 50.96 53.23 48.40 48.72 50.48 51.48 50.61 48.03 50.54 50.11 49.69 50.82 45.58 50.14 49.66 47.79 49.99 52.47 50.68 49.15 51.39 47.67 50.32

Study Design

This dataset has 8 children, one in each row. Each child was given an intervention designed to increase “time on task” in the classroom so that the child will stay focused and complete his or her work. The study has a multiple baseline design. That is, each child was observed for a “baseline” period for a number days before the intervention was implemented. Each child’s baseline lasted a different length of time. Some children were observed for a short time before the intervention, and some were observed much longer. After the intervention ended, each child was observed for a number of days to see if the intervention’s effect diminished.

The id column is just a number to identify each child. The baseline_sessions_n variable indicates how many days the child was observed before the intervention began. The intervention_sessions_n variable indications the number days the intervention lasted. The remainding variables time_1time_60 are the number of minutes the child stayed on task during the observation period each day for 60 days.

Tidy Data

You might think that the data is ready to be analyzed, but it isn’t. The data might be perfectly structured for data entry, but it is not quite “tidy” yet.

The phrase tidy data was popularized by Hadley Wickham, but it mostly refers to something a little less catchy called database normalization. In data analysis, we want:

  1. Each variable must have its own column.
  2. Each observation must have its own row.
  3. Each value must have its own cell.

—From Wickham & Grolemund (2017):

This is how R likes its data. Whenever you find yourself fighting with confusing or tedious tasks in R, there is a good chance your data has violated one of these rules. Converting your data to this “tidy” format will likely simplify your task.

In the current data, there are two primary sources of untidiness.

  • We need a single variable to indicate time on task. Unfortunately, the time on task variable we would like to have is currently spread across 60 variables, time_1time_60.
  • We also would like a variable called “time” to indicate which day the observation took place. Right now, the time variable is lurking in the column names time_1time_60

Later we need to identify which treatment phase the observation was conducted in, “pre-intervention,” “intervention,” or “post-intervention.” That information is sitting in an unusual form right right now in the baseline_sessions_n and intervention_session_n variables.

Data Filtering

The filter function selects rows that meet certain conditions. For example, suppose we want to see rows with baseline_sessions_n less than 8:

The %>% is the “pipe” function. It means “and then.” It inserts the output of the previous function into the next function. If we did not use the pipe, we would type filter(d, baseline_sessions_n < 8) instead.

d %>% 
  filter(baseline_sessions_n < 8)
id baseline_sessions_n intervention_sessions_n time_1 time_2 time_3 time_4 time_5 time_6 time_7 time_8 time_9 time_10 time_11 time_12 time_13 time_14 time_15 time_16 time_17 time_18 time_19 time_20 time_21 time_22 time_23 time_24 time_25 time_26 time_27 time_28 time_29 time_30 time_31 time_32 time_33 time_34 time_35 time_36 time_37 time_38 time_39 time_40 time_41 time_42 time_43 time_44 time_45 time_46 time_47 time_48 time_49 time_50 time_51 time_52 time_53 time_54 time_55 time_56 time_57 time_58 time_59 time_60
3 5 18 7.244 14.31 7.979 11.18 12.29 13.15 11.69 19 16.13 20.3 21.91 21.95 27.11 27.93 27.45 30.75 36.69 37.34 39.2 43.84 44.75 43.36 46.34 47.01 44.5 46.51 44.03 47.02 48.69 43.65 47.7 49.48 48 46.17 48.51 43.8 48.55 48.3 43.9 47.1 51.74 40.91 45.88 49.41 45.65 43.96 46.6 44.57 42.6 47.05 46.58 43.5 42.45 47.22 44.16 49.38 45 46.11 47.09 44.92

In this example, there is little advantage to using the pipe because there is only one step. However, when many steps are strung together, the pipe makes the code much easier to understand.

The double equal sign == performs a test to see if two things are the same. Forgetting that you need two equal signs is a really common error.

Students with id equal to 7:

d %>% 
  filter(id == 7)
id baseline_sessions_n intervention_sessions_n time_1 time_2 time_3 time_4 time_5 time_6 time_7 time_8 time_9 time_10 time_11 time_12 time_13 time_14 time_15 time_16 time_17 time_18 time_19 time_20 time_21 time_22 time_23 time_24 time_25 time_26 time_27 time_28 time_29 time_30 time_31 time_32 time_33 time_34 time_35 time_36 time_37 time_38 time_39 time_40 time_41 time_42 time_43 time_44 time_45 time_46 time_47 time_48 time_49 time_50 time_51 time_52 time_53 time_54 time_55 time_56 time_57 time_58 time_59 time_60
7 10 19 9.695 9.174 10.64 9.136 7.362 14.27 9.448 12.38 6.093 5.854 9.539 14.26 13.73 19.42 20.67 22.58 26.74 24.73 28.85 30.76 31.63 34.36 36.1 40.92 39.43 41.14 43.05 46.76 46.2 48.77 47.37 49.48 52.47 49.37 44.54 48.07 46.38 47.66 45.35 49.1 48.02 47.5 49.15 49 47.31 46.44 49.66 48.07 46.29 45.44 48.75 49.89 49.42 48 50.41 46.83 46.45 48.05 48.95 52.76

You can filter for more than one condition.

Students with scores above 10 on time_1 AND time_2:

d %>% 
  filter(time_1 > 10 & time_2 > 10)
id baseline_sessions_n intervention_sessions_n time_1 time_2 time_3 time_4 time_5 time_6 time_7 time_8 time_9 time_10 time_11 time_12 time_13 time_14 time_15 time_16 time_17 time_18 time_19 time_20 time_21 time_22 time_23 time_24 time_25 time_26 time_27 time_28 time_29 time_30 time_31 time_32 time_33 time_34 time_35 time_36 time_37 time_38 time_39 time_40 time_41 time_42 time_43 time_44 time_45 time_46 time_47 time_48 time_49 time_50 time_51 time_52 time_53 time_54 time_55 time_56 time_57 time_58 time_59 time_60
1 22 20 11.48 14.40 6.151 9.254 9.580 8.160 10.125 11.120 9.203 10.838 9.181 8.436 9.977 13.242 9.317 8.276 9.208 10.89 7.225 11.47 8.570 10.583 11.04 13.13 18.00 12.965 23.840 21.40 22.653 30.187 29.49 31.359 29.32 33.78 38.95 37.28 42.57 37.73 44.34 44.61 49.51 49.60 48.72 50.88 51.23 49.04 51.27 49.00 51.13 48.72 50.64 51.24 54.46 48.84 50.35 54.54 52.77 50.68 50.18 50.03
4 32 20 11.30 14.31 14.811 8.678 9.533 7.823 9.561 8.209 12.898 8.244 8.548 8.616 7.586 8.921 9.199 8.711 11.026 10.80 11.591 10.52 9.027 7.848 12.32 10.07 12.20 7.872 8.831 10.43 8.886 8.553 11.84 7.608 11.14 13.53 14.89 19.39 22.95 23.99 20.03 27.08 28.21 27.32 34.87 33.65 36.78 37.48 40.53 41.81 40.75 45.51 45.75 47.57 56.03 51.27 51.98 47.75 48.65 50.63 48.45 50.34
8 13 20 10.60 11.75 10.903 11.403 8.263 7.589 8.232 7.295 10.028 10.105 9.797 11.889 7.041 12.705 13.626 14.990 19.614 23.58 22.594 25.97 26.860 30.064 29.59 31.23 34.26 36.188 38.330 42.61 43.275 42.504 42.51 51.437 53.72 50.31 48.79 51.41 53.23 50.96 53.23 48.40 48.72 50.48 51.48 50.61 48.03 50.54 50.11 49.69 50.82 45.58 50.14 49.66 47.79 49.99 52.47 50.68 49.15 51.39 47.67 50.32

Students with scores above 10 on time_1 OR time_2:

d %>% 
  filter(time_1 > 10 | time_2 > 10)
id baseline_sessions_n intervention_sessions_n time_1 time_2 time_3 time_4 time_5 time_6 time_7 time_8 time_9 time_10 time_11 time_12 time_13 time_14 time_15 time_16 time_17 time_18 time_19 time_20 time_21 time_22 time_23 time_24 time_25 time_26 time_27 time_28 time_29 time_30 time_31 time_32 time_33 time_34 time_35 time_36 time_37 time_38 time_39 time_40 time_41 time_42 time_43 time_44 time_45 time_46 time_47 time_48 time_49 time_50 time_51 time_52 time_53 time_54 time_55 time_56 time_57 time_58 time_59 time_60
1 22 20 11.481 14.402 6.151 9.254 9.580 8.160 10.125 11.120 9.203 10.838 9.181 8.436 9.977 13.242 9.317 8.276 9.208 10.888 7.225 11.47 8.570 10.583 11.04 13.13 18.00 12.965 23.840 21.40 22.653 30.187 29.49 31.359 29.32 33.78 38.95 37.28 42.57 37.73 44.34 44.61 49.51 49.60 48.72 50.88 51.23 49.04 51.27 49.00 51.13 48.72 50.64 51.24 54.46 48.84 50.35 54.54 52.77 50.68 50.18 50.03
3 5 18 7.244 14.309 7.979 11.175 12.287 13.146 11.694 18.998 16.125 20.296 21.905 21.952 27.114 27.933 27.448 30.747 36.685 37.337 39.197 43.84 44.752 43.358 46.34 47.01 44.50 46.507 44.034 47.02 48.691 43.648 47.70 49.477 48.00 46.17 48.51 43.80 48.55 48.30 43.90 47.10 51.74 40.91 45.88 49.41 45.65 43.96 46.60 44.57 42.60 47.05 46.58 43.50 42.45 47.22 44.16 49.38 45.00 46.11 47.09 44.92
4 32 20 11.297 14.307 14.811 8.678 9.533 7.823 9.561 8.209 12.898 8.244 8.548 8.616 7.586 8.921 9.199 8.711 11.026 10.804 11.591 10.52 9.027 7.848 12.32 10.07 12.20 7.872 8.831 10.43 8.886 8.553 11.84 7.608 11.14 13.53 14.89 19.39 22.95 23.99 20.03 27.08 28.21 27.32 34.87 33.65 36.78 37.48 40.53 41.81 40.75 45.51 45.75 47.57 56.03 51.27 51.98 47.75 48.65 50.63 48.45 50.34
5 19 20 10.667 6.804 8.303 14.498 6.868 12.367 9.414 9.444 8.745 14.305 10.961 10.451 14.828 6.272 10.096 12.403 8.464 8.629 9.839 14.45 13.049 16.131 18.19 17.92 23.90 22.576 23.059 25.57 29.038 31.427 33.96 36.342 38.47 41.44 40.76 44.75 47.46 48.82 52.24 50.29 51.08 48.52 51.01 49.59 50.64 47.94 45.46 52.18 48.01 47.01 50.80 47.71 54.90 49.21 52.97 51.58 47.75 53.56 48.21 50.77
6 24 20 13.027 7.471 13.680 5.769 10.789 8.699 9.323 8.312 12.865 9.967 6.926 7.546 10.175 12.912 5.829 8.554 10.514 12.225 9.299 9.09 9.931 10.490 12.35 11.75 10.45 13.391 16.231 16.95 21.799 21.528 21.88 25.989 26.67 32.07 32.79 35.02 37.48 32.87 35.37 41.40 46.80 49.06 46.94 50.27 54.22 50.03 51.80 49.74 48.78 48.08 48.62 48.07 48.06 46.87 50.26 44.34 51.32 48.04 54.79 48.74
8 13 20 10.596 11.746 10.903 11.403 8.263 7.589 8.232 7.295 10.028 10.105 9.797 11.889 7.041 12.705 13.626 14.990 19.614 23.575 22.594 25.97 26.860 30.064 29.59 31.23 34.26 36.188 38.330 42.61 43.275 42.504 42.51 51.437 53.72 50.31 48.79 51.41 53.23 50.96 53.23 48.40 48.72 50.48 51.48 50.61 48.03 50.54 50.11 49.69 50.82 45.58 50.14 49.66 47.79 49.99 52.47 50.68 49.15 51.39 47.67 50.32

Column selection

We can select columns by naming just the columns we want:

d %>% 
  select(id, time_1)
id time_1
1 11.481
2 9.114
3 7.244
4 11.297
5 10.667
6 13.027
7 9.695
8 10.596

If the columns we want are adjacent, we can use the : operator to select a sequence:

d %>% 
  select(time_1:time_5)
time_1 time_2 time_3 time_4 time_5
11.481 14.402 6.151 9.254 9.580
9.114 8.586 11.010 9.356 8.095
7.244 14.309 7.979 11.175 12.287
11.297 14.307 14.811 8.678 9.533
10.667 6.804 8.303 14.498 6.868
13.027 7.471 13.680 5.769 10.789
9.695 9.174 10.637 9.136 7.362
10.596 11.746 10.903 11.403 8.263

If want everything but a particular column (or columns), use the - operator:

d %>% 
  select(-id)
baseline_sessions_n intervention_sessions_n time_1 time_2 time_3 time_4 time_5 time_6 time_7 time_8 time_9 time_10 time_11 time_12 time_13 time_14 time_15 time_16 time_17 time_18 time_19 time_20 time_21 time_22 time_23 time_24 time_25 time_26 time_27 time_28 time_29 time_30 time_31 time_32 time_33 time_34 time_35 time_36 time_37 time_38 time_39 time_40 time_41 time_42 time_43 time_44 time_45 time_46 time_47 time_48 time_49 time_50 time_51 time_52 time_53 time_54 time_55 time_56 time_57 time_58 time_59 time_60
22 20 11.481 14.402 6.151 9.254 9.580 8.160 10.125 11.120 9.203 10.838 9.181 8.436 9.977 13.242 9.317 8.276 9.208 10.888 7.225 11.47 8.570 10.583 11.04 13.13 18.00 12.965 23.840 21.40 22.653 30.187 29.49 31.359 29.32 33.78 38.95 37.28 42.57 37.73 44.34 44.61 49.51 49.60 48.72 50.88 51.23 49.04 51.27 49.00 51.13 48.72 50.64 51.24 54.46 48.84 50.35 54.54 52.77 50.68 50.18 50.03
18 19 9.114 8.586 11.010 9.356 8.095 8.772 8.953 7.443 11.057 7.590 11.087 10.670 7.843 9.018 11.266 5.886 7.388 8.107 12.111 14.83 13.107 18.156 21.65 22.82 24.85 25.152 29.230 31.50 30.092 34.848 29.83 42.884 40.94 45.12 48.62 48.63 47.46 47.03 48.66 47.54 50.80 50.58 49.93 48.24 49.41 48.22 47.98 47.59 44.75 43.74 49.15 48.02 44.89 48.28 47.23 48.91 44.84 45.47 49.48 50.35
5 18 7.244 14.309 7.979 11.175 12.287 13.146 11.694 18.998 16.125 20.296 21.905 21.952 27.114 27.933 27.448 30.747 36.685 37.337 39.197 43.84 44.752 43.358 46.34 47.01 44.50 46.507 44.034 47.02 48.691 43.648 47.70 49.477 48.00 46.17 48.51 43.80 48.55 48.30 43.90 47.10 51.74 40.91 45.88 49.41 45.65 43.96 46.60 44.57 42.60 47.05 46.58 43.50 42.45 47.22 44.16 49.38 45.00 46.11 47.09 44.92
32 20 11.297 14.307 14.811 8.678 9.533 7.823 9.561 8.209 12.898 8.244 8.548 8.616 7.586 8.921 9.199 8.711 11.026 10.804 11.591 10.52 9.027 7.848 12.32 10.07 12.20 7.872 8.831 10.43 8.886 8.553 11.84 7.608 11.14 13.53 14.89 19.39 22.95 23.99 20.03 27.08 28.21 27.32 34.87 33.65 36.78 37.48 40.53 41.81 40.75 45.51 45.75 47.57 56.03 51.27 51.98 47.75 48.65 50.63 48.45 50.34
19 20 10.667 6.804 8.303 14.498 6.868 12.367 9.414 9.444 8.745 14.305 10.961 10.451 14.828 6.272 10.096 12.403 8.464 8.629 9.839 14.45 13.049 16.131 18.19 17.92 23.90 22.576 23.059 25.57 29.038 31.427 33.96 36.342 38.47 41.44 40.76 44.75 47.46 48.82 52.24 50.29 51.08 48.52 51.01 49.59 50.64 47.94 45.46 52.18 48.01 47.01 50.80 47.71 54.90 49.21 52.97 51.58 47.75 53.56 48.21 50.77
24 20 13.027 7.471 13.680 5.769 10.789 8.699 9.323 8.312 12.865 9.967 6.926 7.546 10.175 12.912 5.829 8.554 10.514 12.225 9.299 9.09 9.931 10.490 12.35 11.75 10.45 13.391 16.231 16.95 21.799 21.528 21.88 25.989 26.67 32.07 32.79 35.02 37.48 32.87 35.37 41.40 46.80 49.06 46.94 50.27 54.22 50.03 51.80 49.74 48.78 48.08 48.62 48.07 48.06 46.87 50.26 44.34 51.32 48.04 54.79 48.74
10 19 9.695 9.174 10.637 9.136 7.362 14.267 9.448 12.378 6.093 5.854 9.539 14.262 13.727 19.425 20.669 22.576 26.741 24.729 28.852 30.76 31.628 34.361 36.10 40.92 39.43 41.137 43.049 46.76 46.199 48.769 47.37 49.477 52.47 49.37 44.54 48.07 46.38 47.66 45.35 49.10 48.02 47.50 49.15 49.00 47.31 46.44 49.66 48.07 46.29 45.44 48.75 49.89 49.42 48.00 50.41 46.83 46.45 48.05 48.95 52.76
13 20 10.596 11.746 10.903 11.403 8.263 7.589 8.232 7.295 10.028 10.105 9.797 11.889 7.041 12.705 13.626 14.990 19.614 23.575 22.594 25.97 26.860 30.064 29.59 31.23 34.26 36.188 38.330 42.61 43.275 42.504 42.51 51.437 53.72 50.31 48.79 51.41 53.23 50.96 53.23 48.40 48.72 50.48 51.48 50.61 48.03 50.54 50.11 49.69 50.82 45.58 50.14 49.66 47.79 49.99 52.47 50.68 49.15 51.39 47.67 50.32

If columns have similar names, we can use the starts_with, ends_width, or contains functions.

Select all variables that start with the phrase time:

d %>% 
  select(starts_with("time"))
time_1 time_2 time_3 time_4 time_5 time_6 time_7 time_8 time_9 time_10 time_11 time_12 time_13 time_14 time_15 time_16 time_17 time_18 time_19 time_20 time_21 time_22 time_23 time_24 time_25 time_26 time_27 time_28 time_29 time_30 time_31 time_32 time_33 time_34 time_35 time_36 time_37 time_38 time_39 time_40 time_41 time_42 time_43 time_44 time_45 time_46 time_47 time_48 time_49 time_50 time_51 time_52 time_53 time_54 time_55 time_56 time_57 time_58 time_59 time_60
11.481 14.402 6.151 9.254 9.580 8.160 10.125 11.120 9.203 10.838 9.181 8.436 9.977 13.242 9.317 8.276 9.208 10.888 7.225 11.47 8.570 10.583 11.04 13.13 18.00 12.965 23.840 21.40 22.653 30.187 29.49 31.359 29.32 33.78 38.95 37.28 42.57 37.73 44.34 44.61 49.51 49.60 48.72 50.88 51.23 49.04 51.27 49.00 51.13 48.72 50.64 51.24 54.46 48.84 50.35 54.54 52.77 50.68 50.18 50.03
9.114 8.586 11.010 9.356 8.095 8.772 8.953 7.443 11.057 7.590 11.087 10.670 7.843 9.018 11.266 5.886 7.388 8.107 12.111 14.83 13.107 18.156 21.65 22.82 24.85 25.152 29.230 31.50 30.092 34.848 29.83 42.884 40.94 45.12 48.62 48.63 47.46 47.03 48.66 47.54 50.80 50.58 49.93 48.24 49.41 48.22 47.98 47.59 44.75 43.74 49.15 48.02 44.89 48.28 47.23 48.91 44.84 45.47 49.48 50.35
7.244 14.309 7.979 11.175 12.287 13.146 11.694 18.998 16.125 20.296 21.905 21.952 27.114 27.933 27.448 30.747 36.685 37.337 39.197 43.84 44.752 43.358 46.34 47.01 44.50 46.507 44.034 47.02 48.691 43.648 47.70 49.477 48.00 46.17 48.51 43.80 48.55 48.30 43.90 47.10 51.74 40.91 45.88 49.41 45.65 43.96 46.60 44.57 42.60 47.05 46.58 43.50 42.45 47.22 44.16 49.38 45.00 46.11 47.09 44.92
11.297 14.307 14.811 8.678 9.533 7.823 9.561 8.209 12.898 8.244 8.548 8.616 7.586 8.921 9.199 8.711 11.026 10.804 11.591 10.52 9.027 7.848 12.32 10.07 12.20 7.872 8.831 10.43 8.886 8.553 11.84 7.608 11.14 13.53 14.89 19.39 22.95 23.99 20.03 27.08 28.21 27.32 34.87 33.65 36.78 37.48 40.53 41.81 40.75 45.51 45.75 47.57 56.03 51.27 51.98 47.75 48.65 50.63 48.45 50.34
10.667 6.804 8.303 14.498 6.868 12.367 9.414 9.444 8.745 14.305 10.961 10.451 14.828 6.272 10.096 12.403 8.464 8.629 9.839 14.45 13.049 16.131 18.19 17.92 23.90 22.576 23.059 25.57 29.038 31.427 33.96 36.342 38.47 41.44 40.76 44.75 47.46 48.82 52.24 50.29 51.08 48.52 51.01 49.59 50.64 47.94 45.46 52.18 48.01 47.01 50.80 47.71 54.90 49.21 52.97 51.58 47.75 53.56 48.21 50.77
13.027 7.471 13.680 5.769 10.789 8.699 9.323 8.312 12.865 9.967 6.926 7.546 10.175 12.912 5.829 8.554 10.514 12.225 9.299 9.09 9.931 10.490 12.35 11.75 10.45 13.391 16.231 16.95 21.799 21.528 21.88 25.989 26.67 32.07 32.79 35.02 37.48 32.87 35.37 41.40 46.80 49.06 46.94 50.27 54.22 50.03 51.80 49.74 48.78 48.08 48.62 48.07 48.06 46.87 50.26 44.34 51.32 48.04 54.79 48.74
9.695 9.174 10.637 9.136 7.362 14.267 9.448 12.378 6.093 5.854 9.539 14.262 13.727 19.425 20.669 22.576 26.741 24.729 28.852 30.76 31.628 34.361 36.10 40.92 39.43 41.137 43.049 46.76 46.199 48.769 47.37 49.477 52.47 49.37 44.54 48.07 46.38 47.66 45.35 49.10 48.02 47.50 49.15 49.00 47.31 46.44 49.66 48.07 46.29 45.44 48.75 49.89 49.42 48.00 50.41 46.83 46.45 48.05 48.95 52.76
10.596 11.746 10.903 11.403 8.263 7.589 8.232 7.295 10.028 10.105 9.797 11.889 7.041 12.705 13.626 14.990 19.614 23.575 22.594 25.97 26.860 30.064 29.59 31.23 34.26 36.188 38.330 42.61 43.275 42.504 42.51 51.437 53.72 50.31 48.79 51.41 53.23 50.96 53.23 48.40 48.72 50.48 51.48 50.61 48.03 50.54 50.11 49.69 50.82 45.58 50.14 49.66 47.79 49.99 52.47 50.68 49.15 51.39 47.67 50.32

Select all variables end with 0:

d %>% 
  select(ends_with("0"))
time_10 time_20 time_30 time_40 time_50 time_60
10.838 11.47 30.187 44.61 48.72 50.03
7.590 14.83 34.848 47.54 43.74 50.35
20.296 43.84 43.648 47.10 47.05 44.92
8.244 10.52 8.553 27.08 45.51 50.34
14.305 14.45 31.427 50.29 47.01 50.77
9.967 9.09 21.528 41.40 48.08 48.74
5.854 30.76 48.769 49.10 45.44 52.76
10.105 25.97 42.504 48.40 45.58 50.32

Select all variables that contain the phrase “_2”

d %>% 
  select(contains("_2"))
time_2 time_20 time_21 time_22 time_23 time_24 time_25 time_26 time_27 time_28 time_29
14.402 11.47 8.570 10.583 11.04 13.13 18.00 12.965 23.840 21.40 22.653
8.586 14.83 13.107 18.156 21.65 22.82 24.85 25.152 29.230 31.50 30.092
14.309 43.84 44.752 43.358 46.34 47.01 44.50 46.507 44.034 47.02 48.691
14.307 10.52 9.027 7.848 12.32 10.07 12.20 7.872 8.831 10.43 8.886
6.804 14.45 13.049 16.131 18.19 17.92 23.90 22.576 23.059 25.57 29.038
7.471 9.09 9.931 10.490 12.35 11.75 10.45 13.391 16.231 16.95 21.799
9.174 30.76 31.628 34.361 36.10 40.92 39.43 41.137 43.049 46.76 46.199
11.746 25.97 26.860 30.064 29.59 31.23 34.26 36.188 38.330 42.61 43.275

The - operator works with these selection functions to exclude variables:

d %>% 
  select(-contains("time_"))
id baseline_sessions_n intervention_sessions_n
1 22 20
2 18 19
3 5 18
4 32 20
5 19 20
6 24 20
7 10 19
8 13 20

Renaming variables

d %>% 
  rename(student_id = id, 
         baseline = baseline_sessions_n)
student_id baseline intervention_sessions_n time_1 time_2 time_3 time_4 time_5 time_6 time_7 time_8 time_9 time_10 time_11 time_12 time_13 time_14 time_15 time_16 time_17 time_18 time_19 time_20 time_21 time_22 time_23 time_24 time_25 time_26 time_27 time_28 time_29 time_30 time_31 time_32 time_33 time_34 time_35 time_36 time_37 time_38 time_39 time_40 time_41 time_42 time_43 time_44 time_45 time_46 time_47 time_48 time_49 time_50 time_51 time_52 time_53 time_54 time_55 time_56 time_57 time_58 time_59 time_60
1 22 20 11.481 14.402 6.151 9.254 9.580 8.160 10.125 11.120 9.203 10.838 9.181 8.436 9.977 13.242 9.317 8.276 9.208 10.888 7.225 11.47 8.570 10.583 11.04 13.13 18.00 12.965 23.840 21.40 22.653 30.187 29.49 31.359 29.32 33.78 38.95 37.28 42.57 37.73 44.34 44.61 49.51 49.60 48.72 50.88 51.23 49.04 51.27 49.00 51.13 48.72 50.64 51.24 54.46 48.84 50.35 54.54 52.77 50.68 50.18 50.03
2 18 19 9.114 8.586 11.010 9.356 8.095 8.772 8.953 7.443 11.057 7.590 11.087 10.670 7.843 9.018 11.266 5.886 7.388 8.107 12.111 14.83 13.107 18.156 21.65 22.82 24.85 25.152 29.230 31.50 30.092 34.848 29.83 42.884 40.94 45.12 48.62 48.63 47.46 47.03 48.66 47.54 50.80 50.58 49.93 48.24 49.41 48.22 47.98 47.59 44.75 43.74 49.15 48.02 44.89 48.28 47.23 48.91 44.84 45.47 49.48 50.35
3 5 18 7.244 14.309 7.979 11.175 12.287 13.146 11.694 18.998 16.125 20.296 21.905 21.952 27.114 27.933 27.448 30.747 36.685 37.337 39.197 43.84 44.752 43.358 46.34 47.01 44.50 46.507 44.034 47.02 48.691 43.648 47.70 49.477 48.00 46.17 48.51 43.80 48.55 48.30 43.90 47.10 51.74 40.91 45.88 49.41 45.65 43.96 46.60 44.57 42.60 47.05 46.58 43.50 42.45 47.22 44.16 49.38 45.00 46.11 47.09 44.92
4 32 20 11.297 14.307 14.811 8.678 9.533 7.823 9.561 8.209 12.898 8.244 8.548 8.616 7.586 8.921 9.199 8.711 11.026 10.804 11.591 10.52 9.027 7.848 12.32 10.07 12.20 7.872 8.831 10.43 8.886 8.553 11.84 7.608 11.14 13.53 14.89 19.39 22.95 23.99 20.03 27.08 28.21 27.32 34.87 33.65 36.78 37.48 40.53 41.81 40.75 45.51 45.75 47.57 56.03 51.27 51.98 47.75 48.65 50.63 48.45 50.34
5 19 20 10.667 6.804 8.303 14.498 6.868 12.367 9.414 9.444 8.745 14.305 10.961 10.451 14.828 6.272 10.096 12.403 8.464 8.629 9.839 14.45 13.049 16.131 18.19 17.92 23.90 22.576 23.059 25.57 29.038 31.427 33.96 36.342 38.47 41.44 40.76 44.75 47.46 48.82 52.24 50.29 51.08 48.52 51.01 49.59 50.64 47.94 45.46 52.18 48.01 47.01 50.80 47.71 54.90 49.21 52.97 51.58 47.75 53.56 48.21 50.77
6 24 20 13.027 7.471 13.680 5.769 10.789 8.699 9.323 8.312 12.865 9.967 6.926 7.546 10.175 12.912 5.829 8.554 10.514 12.225 9.299 9.09 9.931 10.490 12.35 11.75 10.45 13.391 16.231 16.95 21.799 21.528 21.88 25.989 26.67 32.07 32.79 35.02 37.48 32.87 35.37 41.40 46.80 49.06 46.94 50.27 54.22 50.03 51.80 49.74 48.78 48.08 48.62 48.07 48.06 46.87 50.26 44.34 51.32 48.04 54.79 48.74
7 10 19 9.695 9.174 10.637 9.136 7.362 14.267 9.448 12.378 6.093 5.854 9.539 14.262 13.727 19.425 20.669 22.576 26.741 24.729 28.852 30.76 31.628 34.361 36.10 40.92 39.43 41.137 43.049 46.76 46.199 48.769 47.37 49.477 52.47 49.37 44.54 48.07 46.38 47.66 45.35 49.10 48.02 47.50 49.15 49.00 47.31 46.44 49.66 48.07 46.29 45.44 48.75 49.89 49.42 48.00 50.41 46.83 46.45 48.05 48.95 52.76
8 13 20 10.596 11.746 10.903 11.403 8.263 7.589 8.232 7.295 10.028 10.105 9.797 11.889 7.041 12.705 13.626 14.990 19.614 23.575 22.594 25.97 26.860 30.064 29.59 31.23 34.26 36.188 38.330 42.61 43.275 42.504 42.51 51.437 53.72 50.31 48.79 51.41 53.23 50.96 53.23 48.40 48.72 50.48 51.48 50.61 48.03 50.54 50.11 49.69 50.82 45.58 50.14 49.66 47.79 49.99 52.47 50.68 49.15 51.39 47.67 50.32

We can rename many columns at once with a function. The str_replace function usually works like this:

x <- "time_1"
str_replace(x, pattern = "time_", replace = "Time ")
[1] "Time 1"

The rename_with is a function that applies a function to all variable names at once. It can take that function’s arguments, too. For example,

d %>% rename_with(str_replace, pattern = "time_", replace = "Time ")
id baseline_sessions_n intervention_sessions_n Time 1 Time 2 Time 3 Time 4 Time 5 Time 6 Time 7 Time 8 Time 9 Time 10 Time 11 Time 12 Time 13 Time 14 Time 15 Time 16 Time 17 Time 18 Time 19 Time 20 Time 21 Time 22 Time 23 Time 24 Time 25 Time 26 Time 27 Time 28 Time 29 Time 30 Time 31 Time 32 Time 33 Time 34 Time 35 Time 36 Time 37 Time 38 Time 39 Time 40 Time 41 Time 42 Time 43 Time 44 Time 45 Time 46 Time 47 Time 48 Time 49 Time 50 Time 51 Time 52 Time 53 Time 54 Time 55 Time 56 Time 57 Time 58 Time 59 Time 60
1 22 20 11.481 14.402 6.151 9.254 9.580 8.160 10.125 11.120 9.203 10.838 9.181 8.436 9.977 13.242 9.317 8.276 9.208 10.888 7.225 11.47 8.570 10.583 11.04 13.13 18.00 12.965 23.840 21.40 22.653 30.187 29.49 31.359 29.32 33.78 38.95 37.28 42.57 37.73 44.34 44.61 49.51 49.60 48.72 50.88 51.23 49.04 51.27 49.00 51.13 48.72 50.64 51.24 54.46 48.84 50.35 54.54 52.77 50.68 50.18 50.03
2 18 19 9.114 8.586 11.010 9.356 8.095 8.772 8.953 7.443 11.057 7.590 11.087 10.670 7.843 9.018 11.266 5.886 7.388 8.107 12.111 14.83 13.107 18.156 21.65 22.82 24.85 25.152 29.230 31.50 30.092 34.848 29.83 42.884 40.94 45.12 48.62 48.63 47.46 47.03 48.66 47.54 50.80 50.58 49.93 48.24 49.41 48.22 47.98 47.59 44.75 43.74 49.15 48.02 44.89 48.28 47.23 48.91 44.84 45.47 49.48 50.35
3 5 18 7.244 14.309 7.979 11.175 12.287 13.146 11.694 18.998 16.125 20.296 21.905 21.952 27.114 27.933 27.448 30.747 36.685 37.337 39.197 43.84 44.752 43.358 46.34 47.01 44.50 46.507 44.034 47.02 48.691 43.648 47.70 49.477 48.00 46.17 48.51 43.80 48.55 48.30 43.90 47.10 51.74 40.91 45.88 49.41 45.65 43.96 46.60 44.57 42.60 47.05 46.58 43.50 42.45 47.22 44.16 49.38 45.00 46.11 47.09 44.92
4 32 20 11.297 14.307 14.811 8.678 9.533 7.823 9.561 8.209 12.898 8.244 8.548 8.616 7.586 8.921 9.199 8.711 11.026 10.804 11.591 10.52 9.027 7.848 12.32 10.07 12.20 7.872 8.831 10.43 8.886 8.553 11.84 7.608 11.14 13.53 14.89 19.39 22.95 23.99 20.03 27.08 28.21 27.32 34.87 33.65 36.78 37.48 40.53 41.81 40.75 45.51 45.75 47.57 56.03 51.27 51.98 47.75 48.65 50.63 48.45 50.34
5 19 20 10.667 6.804 8.303 14.498 6.868 12.367 9.414 9.444 8.745 14.305 10.961 10.451 14.828 6.272 10.096 12.403 8.464 8.629 9.839 14.45 13.049 16.131 18.19 17.92 23.90 22.576 23.059 25.57 29.038 31.427 33.96 36.342 38.47 41.44 40.76 44.75 47.46 48.82 52.24 50.29 51.08 48.52 51.01 49.59 50.64 47.94 45.46 52.18 48.01 47.01 50.80 47.71 54.90 49.21 52.97 51.58 47.75 53.56 48.21 50.77
6 24 20 13.027 7.471 13.680 5.769 10.789 8.699 9.323 8.312 12.865 9.967 6.926 7.546 10.175 12.912 5.829 8.554 10.514 12.225 9.299 9.09 9.931 10.490 12.35 11.75 10.45 13.391 16.231 16.95 21.799 21.528 21.88 25.989 26.67 32.07 32.79 35.02 37.48 32.87 35.37 41.40 46.80 49.06 46.94 50.27 54.22 50.03 51.80 49.74 48.78 48.08 48.62 48.07 48.06 46.87 50.26 44.34 51.32 48.04 54.79 48.74
7 10 19 9.695 9.174 10.637 9.136 7.362 14.267 9.448 12.378 6.093 5.854 9.539 14.262 13.727 19.425 20.669 22.576 26.741 24.729 28.852 30.76 31.628 34.361 36.10 40.92 39.43 41.137 43.049 46.76 46.199 48.769 47.37 49.477 52.47 49.37 44.54 48.07 46.38 47.66 45.35 49.10 48.02 47.50 49.15 49.00 47.31 46.44 49.66 48.07 46.29 45.44 48.75 49.89 49.42 48.00 50.41 46.83 46.45 48.05 48.95 52.76
8 13 20 10.596 11.746 10.903 11.403 8.263 7.589 8.232 7.295 10.028 10.105 9.797 11.889 7.041 12.705 13.626 14.990 19.614 23.575 22.594 25.97 26.860 30.064 29.59 31.23 34.26 36.188 38.330 42.61 43.275 42.504 42.51 51.437 53.72 50.31 48.79 51.41 53.23 50.96 53.23 48.40 48.72 50.48 51.48 50.61 48.03 50.54 50.11 49.69 50.82 45.58 50.14 49.66 47.79 49.99 52.47 50.68 49.15 51.39 47.67 50.32

If you have a vector with names in it you can rename many variables at once:

v_names <- c(ID = "id", 
             Baseline = "baseline_sessions_n",
             Intervention = "intervention_sessions_n")

d %>% 
  rename(any_of(v_names))
ID Baseline Intervention time_1 time_2 time_3 time_4 time_5 time_6 time_7 time_8 time_9 time_10 time_11 time_12 time_13 time_14 time_15 time_16 time_17 time_18 time_19 time_20 time_21 time_22 time_23 time_24 time_25 time_26 time_27 time_28 time_29 time_30 time_31 time_32 time_33 time_34 time_35 time_36 time_37 time_38 time_39 time_40 time_41 time_42 time_43 time_44 time_45 time_46 time_47 time_48 time_49 time_50 time_51 time_52 time_53 time_54 time_55 time_56 time_57 time_58 time_59 time_60
1 22 20 11.481 14.402 6.151 9.254 9.580 8.160 10.125 11.120 9.203 10.838 9.181 8.436 9.977 13.242 9.317 8.276 9.208 10.888 7.225 11.47 8.570 10.583 11.04 13.13 18.00 12.965 23.840 21.40 22.653 30.187 29.49 31.359 29.32 33.78 38.95 37.28 42.57 37.73 44.34 44.61 49.51 49.60 48.72 50.88 51.23 49.04 51.27 49.00 51.13 48.72 50.64 51.24 54.46 48.84 50.35 54.54 52.77 50.68 50.18 50.03
2 18 19 9.114 8.586 11.010 9.356 8.095 8.772 8.953 7.443 11.057 7.590 11.087 10.670 7.843 9.018 11.266 5.886 7.388 8.107 12.111 14.83 13.107 18.156 21.65 22.82 24.85 25.152 29.230 31.50 30.092 34.848 29.83 42.884 40.94 45.12 48.62 48.63 47.46 47.03 48.66 47.54 50.80 50.58 49.93 48.24 49.41 48.22 47.98 47.59 44.75 43.74 49.15 48.02 44.89 48.28 47.23 48.91 44.84 45.47 49.48 50.35
3 5 18 7.244 14.309 7.979 11.175 12.287 13.146 11.694 18.998 16.125 20.296 21.905 21.952 27.114 27.933 27.448 30.747 36.685 37.337 39.197 43.84 44.752 43.358 46.34 47.01 44.50 46.507 44.034 47.02 48.691 43.648 47.70 49.477 48.00 46.17 48.51 43.80 48.55 48.30 43.90 47.10 51.74 40.91 45.88 49.41 45.65 43.96 46.60 44.57 42.60 47.05 46.58 43.50 42.45 47.22 44.16 49.38 45.00 46.11 47.09 44.92
4 32 20 11.297 14.307 14.811 8.678 9.533 7.823 9.561 8.209 12.898 8.244 8.548 8.616 7.586 8.921 9.199 8.711 11.026 10.804 11.591 10.52 9.027 7.848 12.32 10.07 12.20 7.872 8.831 10.43 8.886 8.553 11.84 7.608 11.14 13.53 14.89 19.39 22.95 23.99 20.03 27.08 28.21 27.32 34.87 33.65 36.78 37.48 40.53 41.81 40.75 45.51 45.75 47.57 56.03 51.27 51.98 47.75 48.65 50.63 48.45 50.34
5 19 20 10.667 6.804 8.303 14.498 6.868 12.367 9.414 9.444 8.745 14.305 10.961 10.451 14.828 6.272 10.096 12.403 8.464 8.629 9.839 14.45 13.049 16.131 18.19 17.92 23.90 22.576 23.059 25.57 29.038 31.427 33.96 36.342 38.47 41.44 40.76 44.75 47.46 48.82 52.24 50.29 51.08 48.52 51.01 49.59 50.64 47.94 45.46 52.18 48.01 47.01 50.80 47.71 54.90 49.21 52.97 51.58 47.75 53.56 48.21 50.77
6 24 20 13.027 7.471 13.680 5.769 10.789 8.699 9.323 8.312 12.865 9.967 6.926 7.546 10.175 12.912 5.829 8.554 10.514 12.225 9.299 9.09 9.931 10.490 12.35 11.75 10.45 13.391 16.231 16.95 21.799 21.528 21.88 25.989 26.67 32.07 32.79 35.02 37.48 32.87 35.37 41.40 46.80 49.06 46.94 50.27 54.22 50.03 51.80 49.74 48.78 48.08 48.62 48.07 48.06 46.87 50.26 44.34 51.32 48.04 54.79 48.74
7 10 19 9.695 9.174 10.637 9.136 7.362 14.267 9.448 12.378 6.093 5.854 9.539 14.262 13.727 19.425 20.669 22.576 26.741 24.729 28.852 30.76 31.628 34.361 36.10 40.92 39.43 41.137 43.049 46.76 46.199 48.769 47.37 49.477 52.47 49.37 44.54 48.07 46.38 47.66 45.35 49.10 48.02 47.50 49.15 49.00 47.31 46.44 49.66 48.07 46.29 45.44 48.75 49.89 49.42 48.00 50.41 46.83 46.45 48.05 48.95 52.76
8 13 20 10.596 11.746 10.903 11.403 8.263 7.589 8.232 7.295 10.028 10.105 9.797 11.889 7.041 12.705 13.626 14.990 19.614 23.575 22.594 25.97 26.860 30.064 29.59 31.23 34.26 36.188 38.330 42.61 43.275 42.504 42.51 51.437 53.72 50.31 48.79 51.41 53.23 50.96 53.23 48.40 48.72 50.48 51.48 50.61 48.03 50.54 50.11 49.69 50.82 45.58 50.14 49.66 47.79 49.99 52.47 50.68 49.15 51.39 47.67 50.32

In this example, there is no advantage over just applying the transformations directly in rename:

d %>% 
  rename(ID = "id", 
         Baseline = "baseline_sessions_n",
         Intervention = "intervention_sessions_n")
ID Baseline Intervention time_1 time_2 time_3 time_4 time_5 time_6 time_7 time_8 time_9 time_10 time_11 time_12 time_13 time_14 time_15 time_16 time_17 time_18 time_19 time_20 time_21 time_22 time_23 time_24 time_25 time_26 time_27 time_28 time_29 time_30 time_31 time_32 time_33 time_34 time_35 time_36 time_37 time_38 time_39 time_40 time_41 time_42 time_43 time_44 time_45 time_46 time_47 time_48 time_49 time_50 time_51 time_52 time_53 time_54 time_55 time_56 time_57 time_58 time_59 time_60
1 22 20 11.481 14.402 6.151 9.254 9.580 8.160 10.125 11.120 9.203 10.838 9.181 8.436 9.977 13.242 9.317 8.276 9.208 10.888 7.225 11.47 8.570 10.583 11.04 13.13 18.00 12.965 23.840 21.40 22.653 30.187 29.49 31.359 29.32 33.78 38.95 37.28 42.57 37.73 44.34 44.61 49.51 49.60 48.72 50.88 51.23 49.04 51.27 49.00 51.13 48.72 50.64 51.24 54.46 48.84 50.35 54.54 52.77 50.68 50.18 50.03
2 18 19 9.114 8.586 11.010 9.356 8.095 8.772 8.953 7.443 11.057 7.590 11.087 10.670 7.843 9.018 11.266 5.886 7.388 8.107 12.111 14.83 13.107 18.156 21.65 22.82 24.85 25.152 29.230 31.50 30.092 34.848 29.83 42.884 40.94 45.12 48.62 48.63 47.46 47.03 48.66 47.54 50.80 50.58 49.93 48.24 49.41 48.22 47.98 47.59 44.75 43.74 49.15 48.02 44.89 48.28 47.23 48.91 44.84 45.47 49.48 50.35
3 5 18 7.244 14.309 7.979 11.175 12.287 13.146 11.694 18.998 16.125 20.296 21.905 21.952 27.114 27.933 27.448 30.747 36.685 37.337 39.197 43.84 44.752 43.358 46.34 47.01 44.50 46.507 44.034 47.02 48.691 43.648 47.70 49.477 48.00 46.17 48.51 43.80 48.55 48.30 43.90 47.10 51.74 40.91 45.88 49.41 45.65 43.96 46.60 44.57 42.60 47.05 46.58 43.50 42.45 47.22 44.16 49.38 45.00 46.11 47.09 44.92
4 32 20 11.297 14.307 14.811 8.678 9.533 7.823 9.561 8.209 12.898 8.244 8.548 8.616 7.586 8.921 9.199 8.711 11.026 10.804 11.591 10.52 9.027 7.848 12.32 10.07 12.20 7.872 8.831 10.43 8.886 8.553 11.84 7.608 11.14 13.53 14.89 19.39 22.95 23.99 20.03 27.08 28.21 27.32 34.87 33.65 36.78 37.48 40.53 41.81 40.75 45.51 45.75 47.57 56.03 51.27 51.98 47.75 48.65 50.63 48.45 50.34
5 19 20 10.667 6.804 8.303 14.498 6.868 12.367 9.414 9.444 8.745 14.305 10.961 10.451 14.828 6.272 10.096 12.403 8.464 8.629 9.839 14.45 13.049 16.131 18.19 17.92 23.90 22.576 23.059 25.57 29.038 31.427 33.96 36.342 38.47 41.44 40.76 44.75 47.46 48.82 52.24 50.29 51.08 48.52 51.01 49.59 50.64 47.94 45.46 52.18 48.01 47.01 50.80 47.71 54.90 49.21 52.97 51.58 47.75 53.56 48.21 50.77
6 24 20 13.027 7.471 13.680 5.769 10.789 8.699 9.323 8.312 12.865 9.967 6.926 7.546 10.175 12.912 5.829 8.554 10.514 12.225 9.299 9.09 9.931 10.490 12.35 11.75 10.45 13.391 16.231 16.95 21.799 21.528 21.88 25.989 26.67 32.07 32.79 35.02 37.48 32.87 35.37 41.40 46.80 49.06 46.94 50.27 54.22 50.03 51.80 49.74 48.78 48.08 48.62 48.07 48.06 46.87 50.26 44.34 51.32 48.04 54.79 48.74
7 10 19 9.695 9.174 10.637 9.136 7.362 14.267 9.448 12.378 6.093 5.854 9.539 14.262 13.727 19.425 20.669 22.576 26.741 24.729 28.852 30.76 31.628 34.361 36.10 40.92 39.43 41.137 43.049 46.76 46.199 48.769 47.37 49.477 52.47 49.37 44.54 48.07 46.38 47.66 45.35 49.10 48.02 47.50 49.15 49.00 47.31 46.44 49.66 48.07 46.29 45.44 48.75 49.89 49.42 48.00 50.41 46.83 46.45 48.05 48.95 52.76
8 13 20 10.596 11.746 10.903 11.403 8.263 7.589 8.232 7.295 10.028 10.105 9.797 11.889 7.041 12.705 13.626 14.990 19.614 23.575 22.594 25.97 26.860 30.064 29.59 31.23 34.26 36.188 38.330 42.61 43.275 42.504 42.51 51.437 53.72 50.31 48.79 51.41 53.23 50.96 53.23 48.40 48.72 50.48 51.48 50.61 48.03 50.54 50.11 49.69 50.82 45.58 50.14 49.66 47.79 49.99 52.47 50.68 49.15 51.39 47.67 50.32

The reason one might use the named-vector method is that one already has the named vector handy, and retyping it would be an error-prone waste of time.

Data Restructuring

We need to make our wide data long. To make the illustration simple to see, let’s make a smaller version of the data. We will select just times 1–3 for the first 2 children:

d_small <- d %>% 
  select(id:time_3) %>% 
  filter(id < 3)

Breaking down each step, the code above does these three things:

  1. Create a new tibble called d_small starting with the tibble d “and then” (i..e, %>%)…
  2. select variables id through time_3 “and then”…
  3. filter the rows to find only the cases in which id is less than 3.

Let’s display d_small:

d_small
id baseline_sessions_n intervention_sessions_n time_1 time_2 time_3
1 22 20 11.481 14.402 6.151
2 18 19 9.114 8.586 11.010

What we need is:

id baseline_sessions_n intervention_sessions_n time on_task
1 22 20 1 11.481
1 22 20 2 14.402
1 22 20 3 6.151
2 18 19 1 9.114
2 18 19 2 8.586
2 18 19 3 11.010

Restructuring from wide to long format with pivot_longer

Use the pivot_longer function to pivot the three time variables to long format:

d_small %>% 
  pivot_longer(cols = time_1:time_3)
id baseline_sessions_n intervention_sessions_n name value
1 22 20 time_1 11.481
1 22 20 time_2 14.402
1 22 20 time_3 6.151
2 18 19 time_1 9.114
2 18 19 time_2 8.586
2 18 19 time_3 11.010

To avoid the hassle of renaming our columns, we can specify what the name and value columns should be called:

d_small %>%
  pivot_longer(cols = time_1:time_3,
               names_to = "time",
               values_to = "on_task")
id baseline_sessions_n intervention_sessions_n time on_task
1 22 20 time_1 11.481
1 22 20 time_2 14.402
1 22 20 time_3 6.151
2 18 19 time_1 9.114
2 18 19 time_2 8.586
2 18 19 time_3 11.010

Notice that the time variable is text, not a number, like we want. There are many ways to get rid of the prefix. The simplest is to tell pivot_longer to strip away the prefix “time_”.

d_small %>%
  pivot_longer(cols = time_1:time_3,
               names_to = "time",
               values_to = "on_task",
               names_prefix = "time_")
id baseline_sessions_n intervention_sessions_n time on_task
1 22 20 1 11.481
1 22 20 2 14.402
1 22 20 3 6.151
2 18 19 1 9.114
2 18 19 2 8.586
2 18 19 3 11.010

Unfortunately, R thinks that time is a text variable. We want it to be an integer, so we transform it using the as.integer function:

d_small %>%
  pivot_longer(cols = time_1:time_3,
               names_to = "time",
               values_to = "on_task",
               names_prefix = "time_", 
               names_transform = list(time = as.integer)) 
id baseline_sessions_n intervention_sessions_n time on_task
1 22 20 1 11.481
1 22 20 2 14.402
1 22 20 3 6.151
2 18 19 1 9.114
2 18 19 2 8.586
2 18 19 3 11.010

Alternatively, we can do all these transformations after pivoting:

d_small %>%
  pivot_longer(cols = time_1:time_3,
               names_to = "time",
               values_to = "on_task") %>% 
  mutate(time = str_remove(time, "time_") %>% 
           as.integer())
id baseline_sessions_n intervention_sessions_n time on_task
1 22 20 1 11.481
1 22 20 2 14.402
1 22 20 3 6.151
2 18 19 1 9.114
2 18 19 2 8.586
2 18 19 3 11.010

Restructuring from long to wide format with pivot_wider

Let’s pivot the data from long format to wide. First let’s assign the restructured d_small data as d_small_longer:

d_small_longer <- d_small %>%
  pivot_longer(cols = time_1:time_3,
               names_to = "time",
               values_to = "on_task",
               names_prefix = "time_",
               names_transform = list(time = as.integer))

Now let’s move it back to where it was with pivot_wider:

d_small_longer %>% 
  pivot_wider(names_from = time, 
              values_from = on_task, 
              names_prefix = "time_")
id baseline_sessions_n intervention_sessions_n time_1 time_2 time_3
1 22 20 11.481 14.402 6.151
2 18 19 9.114 8.586 11.010

Perfect! Now lets move back to the the original large tibble d.

Create new variables with mutate and case_when

First, let’s pivot the entire data and assign it to d_longer. We need to select time_1time_60:

d_longer <- d %>%
  pivot_longer(cols = time_1:time_60,
               names_to = "time",
               values_to = "on_task",
               names_prefix = "time_",
               names_transform = list(time = as.integer))

To create a new variable in d_longer, we “mutate” the tibble. For example, if we want to add 1 to on_task,

d_longer %>% 
  mutate(ontask_plus_1 = on_task + 1)
id baseline_sessions_n intervention_sessions_n time on_task ontask_plus_1
1 22 20 1 11.481 12.481
1 22 20 2 14.402 15.402
1 22 20 3 6.151 7.151
1 22 20 4 9.254 10.254
1 22 20 5 9.580 10.580
1 22 20 6 8.160 9.160
1 22 20 7 10.125 11.125
1 22 20 8 11.120 12.120
1 22 20 9 9.203 10.203
1 22 20 10 10.838 11.838
1 22 20 11 9.181 10.181
1 22 20 12 8.436 9.436
1 22 20 13 9.977 10.977
1 22 20 14 13.242 14.242
1 22 20 15 9.317 10.317
1 22 20 16 8.276 9.276
1 22 20 17 9.208 10.208
1 22 20 18 10.888 11.888
1 22 20 19 7.225 8.225
1 22 20 20 11.472 12.472
1 22 20 21 8.570 9.570
1 22 20 22 10.583 11.583
1 22 20 23 11.038 12.038
1 22 20 24 13.126 14.126
1 22 20 25 17.995 18.995
1 22 20 26 12.965 13.965
1 22 20 27 23.840 24.840
1 22 20 28 21.396 22.396
1 22 20 29 22.653 23.653
1 22 20 30 30.187 31.187
1 22 20 31 29.494 30.494
1 22 20 32 31.359 32.359
1 22 20 33 29.316 30.316
1 22 20 34 33.783 34.783
1 22 20 35 38.945 39.945
1 22 20 36 37.283 38.283
1 22 20 37 42.568 43.568
1 22 20 38 37.734 38.734
1 22 20 39 44.345 45.345
1 22 20 40 44.606 45.606
1 22 20 41 49.511 50.511
1 22 20 42 49.604 50.604
1 22 20 43 48.723 49.723
1 22 20 44 50.882 51.882
1 22 20 45 51.230 52.230
1 22 20 46 49.038 50.038
1 22 20 47 51.275 52.275
1 22 20 48 49.000 50.000
1 22 20 49 51.135 52.135
1 22 20 50 48.722 49.722
1 22 20 51 50.638 51.638
1 22 20 52 51.235 52.235
1 22 20 53 54.457 55.457
1 22 20 54 48.840 49.840
1 22 20 55 50.354 51.354
1 22 20 56 54.538 55.538
1 22 20 57 52.765 53.765
1 22 20 58 50.677 51.677
1 22 20 59 50.179 51.179
1 22 20 60 50.030 51.030
2 18 19 1 9.114 10.114
2 18 19 2 8.586 9.586
2 18 19 3 11.010 12.010
2 18 19 4 9.356 10.356
2 18 19 5 8.095 9.095
2 18 19 6 8.772 9.772
2 18 19 7 8.953 9.953
2 18 19 8 7.443 8.443
2 18 19 9 11.057 12.057
2 18 19 10 7.590 8.590
2 18 19 11 11.087 12.087
2 18 19 12 10.670 11.670
2 18 19 13 7.843 8.843
2 18 19 14 9.018 10.018
2 18 19 15 11.266 12.266
2 18 19 16 5.886 6.886
2 18 19 17 7.388 8.388
2 18 19 18 8.107 9.107
2 18 19 19 12.111 13.111
2 18 19 20 14.825 15.825
2 18 19 21 13.107 14.107
2 18 19 22 18.156 19.156
2 18 19 23 21.652 22.652
2 18 19 24 22.819 23.819
2 18 19 25 24.846 25.846
2 18 19 26 25.152 26.152
2 18 19 27 29.230 30.230
2 18 19 28 31.499 32.499
2 18 19 29 30.092 31.092
2 18 19 30 34.848 35.848
2 18 19 31 29.831 30.831
2 18 19 32 42.884 43.884
2 18 19 33 40.936 41.936
2 18 19 34 45.121 46.121
2 18 19 35 48.622 49.622
2 18 19 36 48.627 49.627
2 18 19 37 47.455 48.455
2 18 19 38 47.031 48.031
2 18 19 39 48.660 49.660
2 18 19 40 47.544 48.544
2 18 19 41 50.799 51.799
2 18 19 42 50.576 51.576
2 18 19 43 49.929 50.929
2 18 19 44 48.240 49.240
2 18 19 45 49.406 50.406
2 18 19 46 48.225 49.225
2 18 19 47 47.975 48.975
2 18 19 48 47.588 48.588
2 18 19 49 44.746 45.746
2 18 19 50 43.743 44.743
2 18 19 51 49.148 50.148
2 18 19 52 48.024 49.024
2 18 19 53 44.893 45.893
2 18 19 54 48.279 49.279
2 18 19 55 47.229 48.229
2 18 19 56 48.910 49.910
2 18 19 57 44.843 45.843
2 18 19 58 45.472 46.472
2 18 19 59 49.480 50.480
2 18 19 60 50.346 51.346
3 5 18 1 7.244 8.244
3 5 18 2 14.309 15.309
3 5 18 3 7.979 8.979
3 5 18 4 11.175 12.175
3 5 18 5 12.287 13.287
3 5 18 6 13.146 14.146
3 5 18 7 11.694 12.694
3 5 18 8 18.998 19.998
3 5 18 9 16.125 17.125
3 5 18 10 20.296 21.296
3 5 18 11 21.905 22.905
3 5 18 12 21.952 22.952
3 5 18 13 27.114 28.114
3 5 18 14 27.933 28.933
3 5 18 15 27.448 28.448
3 5 18 16 30.747 31.747
3 5 18 17 36.685 37.685
3 5 18 18 37.337 38.337
3 5 18 19 39.197 40.197
3 5 18 20 43.845 44.845
3 5 18 21 44.752 45.752
3 5 18 22 43.358 44.358
3 5 18 23 46.341 47.341
3 5 18 24 47.006 48.006
3 5 18 25 44.497 45.497
3 5 18 26 46.507 47.507
3 5 18 27 44.034 45.034
3 5 18 28 47.017 48.017
3 5 18 29 48.691 49.691
3 5 18 30 43.648 44.648
3 5 18 31 47.700 48.700
3 5 18 32 49.477 50.477
3 5 18 33 48.000 49.000
3 5 18 34 46.173 47.173
3 5 18 35 48.513 49.513
3 5 18 36 43.801 44.801
3 5 18 37 48.552 49.552
3 5 18 38 48.298 49.298
3 5 18 39 43.902 44.902
3 5 18 40 47.103 48.103
3 5 18 41 51.735 52.735
3 5 18 42 40.911 41.911
3 5 18 43 45.881 46.881
3 5 18 44 49.407 50.407
3 5 18 45 45.653 46.653
3 5 18 46 43.964 44.964
3 5 18 47 46.602 47.602
3 5 18 48 44.568 45.568
3 5 18 49 42.596 43.596
3 5 18 50 47.055 48.055
3 5 18 51 46.582 47.582
3 5 18 52 43.501 44.501
3 5 18 53 42.446 43.446
3 5 18 54 47.222 48.222
3 5 18 55 44.163 45.163
3 5 18 56 49.381 50.381
3 5 18 57 44.999 45.999
3 5 18 58 46.108 47.108
3 5 18 59 47.088 48.088
3 5 18 60 44.919 45.919
4 32 20 1 11.297 12.297
4 32 20 2 14.307 15.307
4 32 20 3 14.811 15.811
4 32 20 4 8.678 9.678
4 32 20 5 9.533 10.533
4 32 20 6 7.823 8.823
4 32 20 7 9.561 10.561
4 32 20 8 8.209 9.209
4 32 20 9 12.898 13.898
4 32 20 10 8.244 9.244
4 32 20 11 8.548 9.548
4 32 20 12 8.616 9.616
4 32 20 13 7.586 8.586
4 32 20 14 8.921 9.921
4 32 20 15 9.199 10.199
4 32 20 16 8.711 9.711
4 32 20 17 11.026 12.026
4 32 20 18 10.804 11.804
4 32 20 19 11.591 12.591
4 32 20 20 10.519 11.519
4 32 20 21 9.027 10.027
4 32 20 22 7.848 8.848
4 32 20 23 12.319 13.319
4 32 20 24 10.068 11.068
4 32 20 25 12.198 13.198
4 32 20 26 7.872 8.872
4 32 20 27 8.831 9.831
4 32 20 28 10.432 11.432
4 32 20 29 8.886 9.886
4 32 20 30 8.553 9.553
4 32 20 31 11.836 12.836
4 32 20 32 7.608 8.608
4 32 20 33 11.136 12.136
4 32 20 34 13.533 14.533
4 32 20 35 14.891 15.891
4 32 20 36 19.392 20.392
4 32 20 37 22.947 23.947
4 32 20 38 23.988 24.988
4 32 20 39 20.032 21.032
4 32 20 40 27.083 28.083
4 32 20 41 28.207 29.207
4 32 20 42 27.317 28.317
4 32 20 43 34.869 35.869
4 32 20 44 33.646 34.646
4 32 20 45 36.784 37.784
4 32 20 46 37.477 38.477
4 32 20 47 40.527 41.527
4 32 20 48 41.814 42.814
4 32 20 49 40.749 41.749
4 32 20 50 45.507 46.507
4 32 20 51 45.754 46.754
4 32 20 52 47.572 48.572
4 32 20 53 56.028 57.028
4 32 20 54 51.273 52.273
4 32 20 55 51.980 52.980
4 32 20 56 47.747 48.747
4 32 20 57 48.654 49.654
4 32 20 58 50.635 51.635
4 32 20 59 48.446 49.446
4 32 20 60 50.343 51.343
5 19 20 1 10.667 11.667
5 19 20 2 6.804 7.804
5 19 20 3 8.303 9.303
5 19 20 4 14.498 15.498
5 19 20 5 6.868 7.868
5 19 20 6 12.367 13.367
5 19 20 7 9.414 10.414
5 19 20 8 9.444 10.444
5 19 20 9 8.745 9.745
5 19 20 10 14.305 15.305
5 19 20 11 10.961 11.961
5 19 20 12 10.451 11.451
5 19 20 13 14.828 15.828
5 19 20 14 6.272 7.272
5 19 20 15 10.096 11.096
5 19 20 16 12.403 13.403
5 19 20 17 8.464 9.464
5 19 20 18 8.629 9.629
5 19 20 19 9.839 10.839
5 19 20 20 14.449 15.449
5 19 20 21 13.049 14.049
5 19 20 22 16.131 17.131
5 19 20 23 18.193 19.193
5 19 20 24 17.921 18.921
5 19 20 25 23.902 24.902
5 19 20 26 22.576 23.576
5 19 20 27 23.059 24.059
5 19 20 28 25.573 26.573
5 19 20 29 29.038 30.038
5 19 20 30 31.427 32.427
5 19 20 31 33.964 34.964
5 19 20 32 36.342 37.342
5 19 20 33 38.469 39.469
5 19 20 34 41.438 42.438
5 19 20 35 40.760 41.760
5 19 20 36 44.753 45.753
5 19 20 37 47.462 48.462
5 19 20 38 48.816 49.816
5 19 20 39 52.240 53.240
5 19 20 40 50.288 51.288
5 19 20 41 51.077 52.077
5 19 20 42 48.519 49.519
5 19 20 43 51.011 52.011
5 19 20 44 49.589 50.589
5 19 20 45 50.637 51.637
5 19 20 46 47.937 48.937
5 19 20 47 45.461 46.461
5 19 20 48 52.178 53.178
5 19 20 49 48.010 49.010
5 19 20 50 47.008 48.008
5 19 20 51 50.800 51.800
5 19 20 52 47.707 48.707
5 19 20 53 54.898 55.898
5 19 20 54 49.207 50.207
5 19 20 55 52.968 53.968
5 19 20 56 51.581 52.581
5 19 20 57 47.745 48.745
5 19 20 58 53.559 54.559
5 19 20 59 48.210 49.210
5 19 20 60 50.766 51.766
6 24 20 1 13.027 14.027
6 24 20 2 7.471 8.471
6 24 20 3 13.680 14.680
6 24 20 4 5.769 6.769
6 24 20 5 10.789 11.789
6 24 20 6 8.699 9.699
6 24 20 7 9.323 10.323
6 24 20 8 8.312 9.312
6 24 20 9 12.865 13.865
6 24 20 10 9.967 10.967
6 24 20 11 6.926 7.926
6 24 20 12 7.546 8.546
6 24 20 13 10.175 11.175
6 24 20 14 12.912 13.912
6 24 20 15 5.829 6.829
6 24 20 16 8.554 9.554
6 24 20 17 10.514 11.514
6 24 20 18 12.225 13.225
6 24 20 19 9.299 10.299
6 24 20 20 9.090 10.090
6 24 20 21 9.931 10.931
6 24 20 22 10.490 11.490
6 24 20 23 12.348 13.348
6 24 20 24 11.752 12.752
6 24 20 25 10.447 11.447
6 24 20 26 13.391 14.391
6 24 20 27 16.231 17.231
6 24 20 28 16.951 17.951
6 24 20 29 21.799 22.799
6 24 20 30 21.528 22.528
6 24 20 31 21.879 22.879
6 24 20 32 25.989 26.989
6 24 20 33 26.665 27.665
6 24 20 34 32.069 33.069
6 24 20 35 32.795 33.795
6 24 20 36 35.019 36.019
6 24 20 37 37.480 38.480
6 24 20 38 32.867 33.867
6 24 20 39 35.365 36.365
6 24 20 40 41.401 42.401
6 24 20 41 46.800 47.800
6 24 20 42 49.059 50.059
6 24 20 43 46.944 47.944
6 24 20 44 50.266 51.266
6 24 20 45 54.224 55.224
6 24 20 46 50.032 51.032
6 24 20 47 51.803 52.803
6 24 20 48 49.742 50.742
6 24 20 49 48.780 49.780
6 24 20 50 48.083 49.083
6 24 20 51 48.620 49.620
6 24 20 52 48.069 49.069
6 24 20 53 48.062 49.062
6 24 20 54 46.871 47.871
6 24 20 55 50.262 51.262
6 24 20 56 44.337 45.337
6 24 20 57 51.324 52.324
6 24 20 58 48.036 49.036
6 24 20 59 54.790 55.790
6 24 20 60 48.737 49.737
7 10 19 1 9.695 10.695
7 10 19 2 9.174 10.174
7 10 19 3 10.637 11.637
7 10 19 4 9.136 10.136
7 10 19 5 7.362 8.362
7 10 19 6 14.267 15.267
7 10 19 7 9.448 10.448
7 10 19 8 12.378 13.378
7 10 19 9 6.093 7.093
7 10 19 10 5.854 6.854
7 10 19 11 9.539 10.539
7 10 19 12 14.262 15.262
7 10 19 13 13.727 14.727
7 10 19 14 19.425 20.425
7 10 19 15 20.669 21.669
7 10 19 16 22.576 23.576
7 10 19 17 26.741 27.741
7 10 19 18 24.729 25.729
7 10 19 19 28.852 29.852
7 10 19 20 30.760 31.760
7 10 19 21 31.628 32.628
7 10 19 22 34.361 35.361
7 10 19 23 36.095 37.095
7 10 19 24 40.918 41.918
7 10 19 25 39.432 40.432
7 10 19 26 41.137 42.137
7 10 19 27 43.049 44.049
7 10 19 28 46.761 47.761
7 10 19 29 46.199 47.199
7 10 19 30 48.769 49.769
7 10 19 31 47.367 48.367
7 10 19 32 49.477 50.477
7 10 19 33 52.471 53.471
7 10 19 34 49.366 50.366
7 10 19 35 44.544 45.544
7 10 19 36 48.075 49.075
7 10 19 37 46.384 47.384
7 10 19 38 47.661 48.661
7 10 19 39 45.355 46.355
7 10 19 40 49.098 50.098
7 10 19 41 48.016 49.016
7 10 19 42 47.499 48.499
7 10 19 43 49.154 50.154
7 10 19 44 48.998 49.998
7 10 19 45 47.308 48.308
7 10 19 46 46.442 47.442
7 10 19 47 49.660 50.660
7 10 19 48 48.067 49.067
7 10 19 49 46.288 47.288
7 10 19 50 45.444 46.444
7 10 19 51 48.753 49.753
7 10 19 52 49.891 50.891
7 10 19 53 49.418 50.418
7 10 19 54 47.998 48.998
7 10 19 55 50.407 51.407
7 10 19 56 46.834 47.834
7 10 19 57 46.451 47.451
7 10 19 58 48.048 49.048
7 10 19 59 48.951 49.951
7 10 19 60 52.757 53.757
8 13 20 1 10.596 11.596
8 13 20 2 11.746 12.746
8 13 20 3 10.903 11.903
8 13 20 4 11.403 12.403
8 13 20 5 8.263 9.263
8 13 20 6 7.589 8.589
8 13 20 7 8.232 9.232
8 13 20 8 7.295 8.295
8 13 20 9 10.028 11.028
8 13 20 10 10.105 11.105
8 13 20 11 9.797 10.797
8 13 20 12 11.889 12.889
8 13 20 13 7.041 8.041
8 13 20 14 12.705 13.705
8 13 20 15 13.626 14.626
8 13 20 16 14.990 15.990
8 13 20 17 19.614 20.614
8 13 20 18 23.575 24.575
8 13 20 19 22.594 23.594
8 13 20 20 25.971 26.971
8 13 20 21 26.860 27.860
8 13 20 22 30.064 31.064
8 13 20 23 29.586 30.586
8 13 20 24 31.227 32.227
8 13 20 25 34.261 35.261
8 13 20 26 36.188 37.188
8 13 20 27 38.330 39.330
8 13 20 28 42.613 43.613
8 13 20 29 43.275 44.275
8 13 20 30 42.504 43.504
8 13 20 31 42.509 43.509
8 13 20 32 51.437 52.437
8 13 20 33 53.725 54.725
8 13 20 34 50.306 51.306
8 13 20 35 48.788 49.788
8 13 20 36 51.408 52.408
8 13 20 37 53.231 54.231
8 13 20 38 50.961 51.961
8 13 20 39 53.231 54.231
8 13 20 40 48.398 49.398
8 13 20 41 48.722 49.722
8 13 20 42 50.481 51.481
8 13 20 43 51.476 52.476
8 13 20 44 50.614 51.614
8 13 20 45 48.026 49.026
8 13 20 46 50.541 51.541
8 13 20 47 50.111 51.111
8 13 20 48 49.685 50.685
8 13 20 49 50.818 51.818
8 13 20 50 45.583 46.583
8 13 20 51 50.140 51.140
8 13 20 52 49.661 50.661
8 13 20 53 47.786 48.786
8 13 20 54 49.989 50.989
8 13 20 55 52.467 53.467
8 13 20 56 50.677 51.677
8 13 20 57 49.147 50.147
8 13 20 58 51.386 52.386
8 13 20 59 47.671 48.671
8 13 20 60 50.322 51.322

Notice that the variable is not saved anywhere because we did not assign the output to a variable:

d_longer
id baseline_sessions_n intervention_sessions_n time on_task
1 22 20 1 11.481
1 22 20 2 14.402
1 22 20 3 6.151
1 22 20 4 9.254
1 22 20 5 9.580
1 22 20 6 8.160
1 22 20 7 10.125
1 22 20 8 11.120
1 22 20 9 9.203
1 22 20 10 10.838
1 22 20 11 9.181
1 22 20 12 8.436
1 22 20 13 9.977
1 22 20 14 13.242
1 22 20 15 9.317
1 22 20 16 8.276
1 22 20 17 9.208
1 22 20 18 10.888
1 22 20 19 7.225
1 22 20 20 11.472
1 22 20 21 8.570
1 22 20 22 10.583
1 22 20 23 11.038
1 22 20 24 13.126
1 22 20 25 17.995
1 22 20 26 12.965
1 22 20 27 23.840
1 22 20 28 21.396
1 22 20 29 22.653
1 22 20 30 30.187
1 22 20 31 29.494
1 22 20 32 31.359
1 22 20 33 29.316
1 22 20 34 33.783
1 22 20 35 38.945
1 22 20 36 37.283
1 22 20 37 42.568
1 22 20 38 37.734
1 22 20 39 44.345
1 22 20 40 44.606
1 22 20 41 49.511
1 22 20 42 49.604
1 22 20 43 48.723
1 22 20 44 50.882
1 22 20 45 51.230
1 22 20 46 49.038
1 22 20 47 51.275
1 22 20 48 49.000
1 22 20 49 51.135
1 22 20 50 48.722
1 22 20 51 50.638
1 22 20 52 51.235
1 22 20 53 54.457
1 22 20 54 48.840
1 22 20 55 50.354
1 22 20 56 54.538
1 22 20 57 52.765
1 22 20 58 50.677
1 22 20 59 50.179
1 22 20 60 50.030
2 18 19 1 9.114
2 18 19 2 8.586
2 18 19 3 11.010
2 18 19 4 9.356
2 18 19 5 8.095
2 18 19 6 8.772
2 18 19 7 8.953
2 18 19 8 7.443
2 18 19 9 11.057
2 18 19 10 7.590
2 18 19 11 11.087
2 18 19 12 10.670
2 18 19 13 7.843
2 18 19 14 9.018
2 18 19 15 11.266
2 18 19 16 5.886
2 18 19 17 7.388
2 18 19 18 8.107
2 18 19 19 12.111
2 18 19 20 14.825
2 18 19 21 13.107
2 18 19 22 18.156
2 18 19 23 21.652
2 18 19 24 22.819
2 18 19 25 24.846
2 18 19 26 25.152
2 18 19 27 29.230
2 18 19 28 31.499
2 18 19 29 30.092
2 18 19 30 34.848
2 18 19 31 29.831
2 18 19 32 42.884
2 18 19 33 40.936
2 18 19 34 45.121
2 18 19 35 48.622
2 18 19 36 48.627
2 18 19 37 47.455
2 18 19 38 47.031
2 18 19 39 48.660
2 18 19 40 47.544
2 18 19 41 50.799
2 18 19 42 50.576
2 18 19 43 49.929
2 18 19 44 48.240
2 18 19 45 49.406
2 18 19 46 48.225
2 18 19 47 47.975
2 18 19 48 47.588
2 18 19 49 44.746
2 18 19 50 43.743
2 18 19 51 49.148
2 18 19 52 48.024
2 18 19 53 44.893
2 18 19 54 48.279
2 18 19 55 47.229
2 18 19 56 48.910
2 18 19 57 44.843
2 18 19 58 45.472
2 18 19 59 49.480
2 18 19 60 50.346
3 5 18 1 7.244
3 5 18 2 14.309
3 5 18 3 7.979
3 5 18 4 11.175
3 5 18 5 12.287
3 5 18 6 13.146
3 5 18 7 11.694
3 5 18 8 18.998
3 5 18 9 16.125
3 5 18 10 20.296
3 5 18 11 21.905
3 5 18 12 21.952
3 5 18 13 27.114
3 5 18 14 27.933
3 5 18 15 27.448
3 5 18 16 30.747
3 5 18 17 36.685
3 5 18 18 37.337
3 5 18 19 39.197
3 5 18 20 43.845
3 5 18 21 44.752
3 5 18 22 43.358
3 5 18 23 46.341
3 5 18 24 47.006
3 5 18 25 44.497
3 5 18 26 46.507
3 5 18 27 44.034
3 5 18 28 47.017
3 5 18 29 48.691
3 5 18 30 43.648
3 5 18 31 47.700
3 5 18 32 49.477
3 5 18 33 48.000
3 5 18 34 46.173
3 5 18 35 48.513
3 5 18 36 43.801
3 5 18 37 48.552
3 5 18 38 48.298
3 5 18 39 43.902
3 5 18 40 47.103
3 5 18 41 51.735
3 5 18 42 40.911
3 5 18 43 45.881
3 5 18 44 49.407
3 5 18 45 45.653
3 5 18 46 43.964
3 5 18 47 46.602
3 5 18 48 44.568
3 5 18 49 42.596
3 5 18 50 47.055
3 5 18 51 46.582
3 5 18 52 43.501
3 5 18 53 42.446
3 5 18 54 47.222
3 5 18 55 44.163
3 5 18 56 49.381
3 5 18 57 44.999
3 5 18 58 46.108
3 5 18 59 47.088
3 5 18 60 44.919
4 32 20 1 11.297
4 32 20 2 14.307
4 32 20 3 14.811
4 32 20 4 8.678
4 32 20 5 9.533
4 32 20 6 7.823
4 32 20 7 9.561
4 32 20 8 8.209
4 32 20 9 12.898
4 32 20 10 8.244
4 32 20 11 8.548
4 32 20 12 8.616
4 32 20 13 7.586
4 32 20 14 8.921
4 32 20 15 9.199
4 32 20 16 8.711
4 32 20 17 11.026
4 32 20 18 10.804
4 32 20 19 11.591
4 32 20 20 10.519
4 32 20 21 9.027
4 32 20 22 7.848
4 32 20 23 12.319
4 32 20 24 10.068
4 32 20 25 12.198
4 32 20 26 7.872
4 32 20 27 8.831
4 32 20 28 10.432
4 32 20 29 8.886
4 32 20 30 8.553
4 32 20 31 11.836
4 32 20 32 7.608
4 32 20 33 11.136
4 32 20 34 13.533
4 32 20 35 14.891
4 32 20 36 19.392
4 32 20 37 22.947
4 32 20 38 23.988
4 32 20 39 20.032
4 32 20 40 27.083
4 32 20 41 28.207
4 32 20 42 27.317
4 32 20 43 34.869
4 32 20 44 33.646
4 32 20 45 36.784
4 32 20 46 37.477
4 32 20 47 40.527
4 32 20 48 41.814
4 32 20 49 40.749
4 32 20 50 45.507
4 32 20 51 45.754
4 32 20 52 47.572
4 32 20 53 56.028
4 32 20 54 51.273
4 32 20 55 51.980
4 32 20 56 47.747
4 32 20 57 48.654
4 32 20 58 50.635
4 32 20 59 48.446
4 32 20 60 50.343
5 19 20 1 10.667
5 19 20 2 6.804
5 19 20 3 8.303
5 19 20 4 14.498
5 19 20 5 6.868
5 19 20 6 12.367
5 19 20 7 9.414
5 19 20 8 9.444
5 19 20 9 8.745
5 19 20 10 14.305
5 19 20 11 10.961
5 19 20 12 10.451
5 19 20 13 14.828
5 19 20 14 6.272
5 19 20 15 10.096
5 19 20 16 12.403
5 19 20 17 8.464
5 19 20 18 8.629
5 19 20 19 9.839
5 19 20 20 14.449
5 19 20 21 13.049
5 19 20 22 16.131
5 19 20 23 18.193
5 19 20 24 17.921
5 19 20 25 23.902
5 19 20 26 22.576
5 19 20 27 23.059
5 19 20 28 25.573
5 19 20 29 29.038
5 19 20 30 31.427
5 19 20 31 33.964
5 19 20 32 36.342
5 19 20 33 38.469
5 19 20 34 41.438
5 19 20 35 40.760
5 19 20 36 44.753
5 19 20 37 47.462
5 19 20 38 48.816
5 19 20 39 52.240
5 19 20 40 50.288
5 19 20 41 51.077
5 19 20 42 48.519
5 19 20 43 51.011
5 19 20 44 49.589
5 19 20 45 50.637
5 19 20 46 47.937
5 19 20 47 45.461
5 19 20 48 52.178
5 19 20 49 48.010
5 19 20 50 47.008
5 19 20 51 50.800
5 19 20 52 47.707
5 19 20 53 54.898
5 19 20 54 49.207
5 19 20 55 52.968
5 19 20 56 51.581
5 19 20 57 47.745
5 19 20 58 53.559
5 19 20 59 48.210
5 19 20 60 50.766
6 24 20 1 13.027
6 24 20 2 7.471
6 24 20 3 13.680
6 24 20 4 5.769
6 24 20 5 10.789
6 24 20 6 8.699
6 24 20 7 9.323
6 24 20 8 8.312
6 24 20 9 12.865
6 24 20 10 9.967
6 24 20 11 6.926
6 24 20 12 7.546
6 24 20 13 10.175
6 24 20 14 12.912
6 24 20 15 5.829
6 24 20 16 8.554
6 24 20 17 10.514
6 24 20 18 12.225
6 24 20 19 9.299
6 24 20 20 9.090
6 24 20 21 9.931
6 24 20 22 10.490
6 24 20 23 12.348
6 24 20 24 11.752
6 24 20 25 10.447
6 24 20 26 13.391
6 24 20 27 16.231
6 24 20 28 16.951
6 24 20 29 21.799
6 24 20 30 21.528
6 24 20 31 21.879
6 24 20 32 25.989
6 24 20 33 26.665
6 24 20 34 32.069
6 24 20 35 32.795
6 24 20 36 35.019
6 24 20 37 37.480
6 24 20 38 32.867
6 24 20 39 35.365
6 24 20 40 41.401
6 24 20 41 46.800
6 24 20 42 49.059
6 24 20 43 46.944
6 24 20 44 50.266
6 24 20 45 54.224
6 24 20 46 50.032
6 24 20 47 51.803
6 24 20 48 49.742
6 24 20 49 48.780
6 24 20 50 48.083
6 24 20 51 48.620
6 24 20 52 48.069
6 24 20 53 48.062
6 24 20 54 46.871
6 24 20 55 50.262
6 24 20 56 44.337
6 24 20 57 51.324
6 24 20 58 48.036
6 24 20 59 54.790
6 24 20 60 48.737
7 10 19 1 9.695
7 10 19 2 9.174
7 10 19 3 10.637
7 10 19 4 9.136
7 10 19 5 7.362
7 10 19 6 14.267
7 10 19 7 9.448
7 10 19 8 12.378
7 10 19 9 6.093
7 10 19 10 5.854
7 10 19 11 9.539
7 10 19 12 14.262
7 10 19 13 13.727
7 10 19 14 19.425
7 10 19 15 20.669
7 10 19 16 22.576
7 10 19 17 26.741
7 10 19 18 24.729
7 10 19 19 28.852
7 10 19 20 30.760
7 10 19 21 31.628
7 10 19 22 34.361
7 10 19 23 36.095
7 10 19 24 40.918
7 10 19 25 39.432
7 10 19 26 41.137
7 10 19 27 43.049
7 10 19 28 46.761
7 10 19 29 46.199
7 10 19 30 48.769
7 10 19 31 47.367
7 10 19 32 49.477
7 10 19 33 52.471
7 10 19 34 49.366
7 10 19 35 44.544
7 10 19 36 48.075
7 10 19 37 46.384
7 10 19 38 47.661
7 10 19 39 45.355
7 10 19 40 49.098
7 10 19 41 48.016
7 10 19 42 47.499
7 10 19 43 49.154
7 10 19 44 48.998
7 10 19 45 47.308
7 10 19 46 46.442
7 10 19 47 49.660
7 10 19 48 48.067
7 10 19 49 46.288
7 10 19 50 45.444
7 10 19 51 48.753
7 10 19 52 49.891
7 10 19 53 49.418
7 10 19 54 47.998
7 10 19 55 50.407
7 10 19 56 46.834
7 10 19 57 46.451
7 10 19 58 48.048
7 10 19 59 48.951
7 10 19 60 52.757
8 13 20 1 10.596
8 13 20 2 11.746
8 13 20 3 10.903
8 13 20 4 11.403
8 13 20 5 8.263
8 13 20 6 7.589
8 13 20 7 8.232
8 13 20 8 7.295
8 13 20 9 10.028
8 13 20 10 10.105
8 13 20 11 9.797
8 13 20 12 11.889
8 13 20 13 7.041
8 13 20 14 12.705
8 13 20 15 13.626
8 13 20 16 14.990
8 13 20 17 19.614
8 13 20 18 23.575
8 13 20 19 22.594
8 13 20 20 25.971
8 13 20 21 26.860
8 13 20 22 30.064
8 13 20 23 29.586
8 13 20 24 31.227
8 13 20 25 34.261
8 13 20 26 36.188
8 13 20 27 38.330
8 13 20 28 42.613
8 13 20 29 43.275
8 13 20 30 42.504
8 13 20 31 42.509
8 13 20 32 51.437
8 13 20 33 53.725
8 13 20 34 50.306
8 13 20 35 48.788
8 13 20 36 51.408
8 13 20 37 53.231
8 13 20 38 50.961
8 13 20 39 53.231
8 13 20 40 48.398
8 13 20 41 48.722
8 13 20 42 50.481
8 13 20 43 51.476
8 13 20 44 50.614
8 13 20 45 48.026
8 13 20 46 50.541
8 13 20 47 50.111
8 13 20 48 49.685
8 13 20 49 50.818
8 13 20 50 45.583
8 13 20 51 50.140
8 13 20 52 49.661
8 13 20 53 47.786
8 13 20 54 49.989
8 13 20 55 52.467
8 13 20 56 50.677
8 13 20 57 49.147
8 13 20 58 51.386
8 13 20 59 47.671
8 13 20 60 50.322

We need to identify which rows are part of the pre-intervention phase, which are in the intervention phase, and which are in the post-intervention phase. Let’s use the case_when function, which is like a series of “if-then” statements followed by the desired result.

d_longer <- d_longer %>% 
  mutate(phase = case_when(
    time <= baseline_sessions_n ~ "Pre-Intervention",
    time <= baseline_sessions_n + intervention_sessions_n ~ "Intervention",
    time > baseline_sessions_n + intervention_sessions_n ~ "Post-Intervention" 
  ))

d_longer
id baseline_sessions_n intervention_sessions_n time on_task phase
1 22 20 1 11.481 Pre-Intervention
1 22 20 2 14.402 Pre-Intervention
1 22 20 3 6.151 Pre-Intervention
1 22 20 4 9.254 Pre-Intervention
1 22 20 5 9.580 Pre-Intervention
1 22 20 6 8.160 Pre-Intervention
1 22 20 7 10.125 Pre-Intervention
1 22 20 8 11.120 Pre-Intervention
1 22 20 9 9.203 Pre-Intervention
1 22 20 10 10.838 Pre-Intervention
1 22 20 11 9.181 Pre-Intervention
1 22 20 12 8.436 Pre-Intervention
1 22 20 13 9.977 Pre-Intervention
1 22 20 14 13.242 Pre-Intervention
1 22 20 15 9.317 Pre-Intervention
1 22 20 16 8.276 Pre-Intervention
1 22 20 17 9.208 Pre-Intervention
1 22 20 18 10.888 Pre-Intervention
1 22 20 19 7.225 Pre-Intervention
1 22 20 20 11.472 Pre-Intervention
1 22 20 21 8.570 Pre-Intervention
1 22 20 22 10.583 Pre-Intervention
1 22 20 23 11.038 Intervention
1 22 20 24 13.126 Intervention
1 22 20 25 17.995 Intervention
1 22 20 26 12.965 Intervention
1 22 20 27 23.840 Intervention
1 22 20 28 21.396 Intervention
1 22 20 29 22.653 Intervention
1 22 20 30 30.187 Intervention
1 22 20 31 29.494 Intervention
1 22 20 32 31.359 Intervention
1 22 20 33 29.316 Intervention
1 22 20 34 33.783 Intervention
1 22 20 35 38.945 Intervention
1 22 20 36 37.283 Intervention
1 22 20 37 42.568 Intervention
1 22 20 38 37.734 Intervention
1 22 20 39 44.345 Intervention
1 22 20 40 44.606 Intervention
1 22 20 41 49.511 Intervention
1 22 20 42 49.604 Intervention
1 22 20 43 48.723 Post-Intervention
1 22 20 44 50.882 Post-Intervention
1 22 20 45 51.230 Post-Intervention
1 22 20 46 49.038 Post-Intervention
1 22 20 47 51.275 Post-Intervention
1 22 20 48 49.000 Post-Intervention
1 22 20 49 51.135 Post-Intervention
1 22 20 50 48.722 Post-Intervention
1 22 20 51 50.638 Post-Intervention
1 22 20 52 51.235 Post-Intervention
1 22 20 53 54.457 Post-Intervention
1 22 20 54 48.840 Post-Intervention
1 22 20 55 50.354 Post-Intervention
1 22 20 56 54.538 Post-Intervention
1 22 20 57 52.765 Post-Intervention
1 22 20 58 50.677 Post-Intervention
1 22 20 59 50.179 Post-Intervention
1 22 20 60 50.030 Post-Intervention
2 18 19 1 9.114 Pre-Intervention
2 18 19 2 8.586 Pre-Intervention
2 18 19 3 11.010 Pre-Intervention
2 18 19 4 9.356 Pre-Intervention
2 18 19 5 8.095 Pre-Intervention
2 18 19 6 8.772 Pre-Intervention
2 18 19 7 8.953 Pre-Intervention
2 18 19 8 7.443 Pre-Intervention
2 18 19 9 11.057 Pre-Intervention
2 18 19 10 7.590 Pre-Intervention
2 18 19 11 11.087 Pre-Intervention
2 18 19 12 10.670 Pre-Intervention
2 18 19 13 7.843 Pre-Intervention
2 18 19 14 9.018 Pre-Intervention
2 18 19 15 11.266 Pre-Intervention
2 18 19 16 5.886 Pre-Intervention
2 18 19 17 7.388 Pre-Intervention
2 18 19 18 8.107 Pre-Intervention
2 18 19 19 12.111 Intervention
2 18 19 20 14.825 Intervention
2 18 19 21 13.107 Intervention
2 18 19 22 18.156 Intervention
2 18 19 23 21.652 Intervention
2 18 19 24 22.819 Intervention
2 18 19 25 24.846 Intervention
2 18 19 26 25.152 Intervention
2 18 19 27 29.230 Intervention
2 18 19 28 31.499 Intervention
2 18 19 29 30.092 Intervention
2 18 19 30 34.848 Intervention
2 18 19 31 29.831 Intervention
2 18 19 32 42.884 Intervention
2 18 19 33 40.936 Intervention
2 18 19 34 45.121 Intervention
2 18 19 35 48.622 Intervention
2 18 19 36 48.627 Intervention
2 18 19 37 47.455 Intervention
2 18 19 38 47.031 Post-Intervention
2 18 19 39 48.660 Post-Intervention
2 18 19 40 47.544 Post-Intervention
2 18 19 41 50.799 Post-Intervention
2 18 19 42 50.576 Post-Intervention
2 18 19 43 49.929 Post-Intervention
2 18 19 44 48.240 Post-Intervention
2 18 19 45 49.406 Post-Intervention
2 18 19 46 48.225 Post-Intervention
2 18 19 47 47.975 Post-Intervention
2 18 19 48 47.588 Post-Intervention
2 18 19 49 44.746 Post-Intervention
2 18 19 50 43.743 Post-Intervention
2 18 19 51 49.148 Post-Intervention
2 18 19 52 48.024 Post-Intervention
2 18 19 53 44.893 Post-Intervention
2 18 19 54 48.279 Post-Intervention
2 18 19 55 47.229 Post-Intervention
2 18 19 56 48.910 Post-Intervention
2 18 19 57 44.843 Post-Intervention
2 18 19 58 45.472 Post-Intervention
2 18 19 59 49.480 Post-Intervention
2 18 19 60 50.346 Post-Intervention
3 5 18 1 7.244 Pre-Intervention
3 5 18 2 14.309 Pre-Intervention
3 5 18 3 7.979 Pre-Intervention
3 5 18 4 11.175 Pre-Intervention
3 5 18 5 12.287 Pre-Intervention
3 5 18 6 13.146 Intervention
3 5 18 7 11.694 Intervention
3 5 18 8 18.998 Intervention
3 5 18 9 16.125 Intervention
3 5 18 10 20.296 Intervention
3 5 18 11 21.905 Intervention
3 5 18 12 21.952 Intervention
3 5 18 13 27.114 Intervention
3 5 18 14 27.933 Intervention
3 5 18 15 27.448 Intervention
3 5 18 16 30.747 Intervention
3 5 18 17 36.685 Intervention
3 5 18 18 37.337 Intervention
3 5 18 19 39.197 Intervention
3 5 18 20 43.845 Intervention
3 5 18 21 44.752 Intervention
3 5 18 22 43.358 Intervention
3 5 18 23 46.341 Intervention
3 5 18 24 47.006 Post-Intervention
3 5 18 25 44.497 Post-Intervention
3 5 18 26 46.507 Post-Intervention
3 5 18 27 44.034 Post-Intervention
3 5 18 28 47.017 Post-Intervention
3 5 18 29 48.691 Post-Intervention
3 5 18 30 43.648 Post-Intervention
3 5 18 31 47.700 Post-Intervention
3 5 18 32 49.477 Post-Intervention
3 5 18 33 48.000 Post-Intervention
3 5 18 34 46.173 Post-Intervention
3 5 18 35 48.513 Post-Intervention
3 5 18 36 43.801 Post-Intervention
3 5 18 37 48.552 Post-Intervention
3 5 18 38 48.298 Post-Intervention
3 5 18 39 43.902 Post-Intervention
3 5 18 40 47.103 Post-Intervention
3 5 18 41 51.735 Post-Intervention
3 5 18 42 40.911 Post-Intervention
3 5 18 43 45.881 Post-Intervention
3 5 18 44 49.407 Post-Intervention
3 5 18 45 45.653 Post-Intervention
3 5 18 46 43.964 Post-Intervention
3 5 18 47 46.602 Post-Intervention
3 5 18 48 44.568 Post-Intervention
3 5 18 49 42.596 Post-Intervention
3 5 18 50 47.055 Post-Intervention
3 5 18 51 46.582 Post-Intervention
3 5 18 52 43.501 Post-Intervention
3 5 18 53 42.446 Post-Intervention
3 5 18 54 47.222 Post-Intervention
3 5 18 55 44.163 Post-Intervention
3 5 18 56 49.381 Post-Intervention
3 5 18 57 44.999 Post-Intervention
3 5 18 58 46.108 Post-Intervention
3 5 18 59 47.088 Post-Intervention
3 5 18 60 44.919 Post-Intervention
4 32 20 1 11.297 Pre-Intervention
4 32 20 2 14.307 Pre-Intervention
4 32 20 3 14.811 Pre-Intervention
4 32 20 4 8.678 Pre-Intervention
4 32 20 5 9.533 Pre-Intervention
4 32 20 6 7.823 Pre-Intervention
4 32 20 7 9.561 Pre-Intervention
4 32 20 8 8.209 Pre-Intervention
4 32 20 9 12.898 Pre-Intervention
4 32 20 10 8.244 Pre-Intervention
4 32 20 11 8.548 Pre-Intervention
4 32 20 12 8.616 Pre-Intervention
4 32 20 13 7.586 Pre-Intervention
4 32 20 14 8.921 Pre-Intervention
4 32 20 15 9.199 Pre-Intervention
4 32 20 16 8.711 Pre-Intervention
4 32 20 17 11.026 Pre-Intervention
4 32 20 18 10.804 Pre-Intervention
4 32 20 19 11.591 Pre-Intervention
4 32 20 20 10.519 Pre-Intervention
4 32 20 21 9.027 Pre-Intervention
4 32 20 22 7.848 Pre-Intervention
4 32 20 23 12.319 Pre-Intervention
4 32 20 24 10.068 Pre-Intervention
4 32 20 25 12.198 Pre-Intervention
4 32 20 26 7.872 Pre-Intervention
4 32 20 27 8.831 Pre-Intervention
4 32 20 28 10.432 Pre-Intervention
4 32 20 29 8.886 Pre-Intervention
4 32 20 30 8.553 Pre-Intervention
4 32 20 31 11.836 Pre-Intervention
4 32 20 32 7.608 Pre-Intervention
4 32 20 33 11.136 Intervention
4 32 20 34 13.533 Intervention
4 32 20 35 14.891 Intervention
4 32 20 36 19.392 Intervention
4 32 20 37 22.947 Intervention
4 32 20 38 23.988 Intervention
4 32 20 39 20.032 Intervention
4 32 20 40 27.083 Intervention
4 32 20 41 28.207 Intervention
4 32 20 42 27.317 Intervention
4 32 20 43 34.869 Intervention
4 32 20 44 33.646 Intervention
4 32 20 45 36.784 Intervention
4 32 20 46 37.477 Intervention
4 32 20 47 40.527 Intervention
4 32 20 48 41.814 Intervention
4 32 20 49 40.749 Intervention
4 32 20 50 45.507 Intervention
4 32 20 51 45.754 Intervention
4 32 20 52 47.572 Intervention
4 32 20 53 56.028 Post-Intervention
4 32 20 54 51.273 Post-Intervention
4 32 20 55 51.980 Post-Intervention
4 32 20 56 47.747 Post-Intervention
4 32 20 57 48.654 Post-Intervention
4 32 20 58 50.635 Post-Intervention
4 32 20 59 48.446 Post-Intervention
4 32 20 60 50.343 Post-Intervention
5 19 20 1 10.667 Pre-Intervention
5 19 20 2 6.804 Pre-Intervention
5 19 20 3 8.303 Pre-Intervention
5 19 20 4 14.498 Pre-Intervention
5 19 20 5 6.868 Pre-Intervention
5 19 20 6 12.367 Pre-Intervention
5 19 20 7 9.414 Pre-Intervention
5 19 20 8 9.444 Pre-Intervention
5 19 20 9 8.745 Pre-Intervention
5 19 20 10 14.305 Pre-Intervention
5 19 20 11 10.961 Pre-Intervention
5 19 20 12 10.451 Pre-Intervention
5 19 20 13 14.828 Pre-Intervention
5 19 20 14 6.272 Pre-Intervention
5 19 20 15 10.096 Pre-Intervention
5 19 20 16 12.403 Pre-Intervention
5 19 20 17 8.464 Pre-Intervention
5 19 20 18 8.629 Pre-Intervention
5 19 20 19 9.839 Pre-Intervention
5 19 20 20 14.449 Intervention
5 19 20 21 13.049 Intervention
5 19 20 22 16.131 Intervention
5 19 20 23 18.193 Intervention
5 19 20 24 17.921 Intervention
5 19 20 25 23.902 Intervention
5 19 20 26 22.576 Intervention
5 19 20 27 23.059 Intervention
5 19 20 28 25.573 Intervention
5 19 20 29 29.038 Intervention
5 19 20 30 31.427 Intervention
5 19 20 31 33.964 Intervention
5 19 20 32 36.342 Intervention
5 19 20 33 38.469 Intervention
5 19 20 34 41.438 Intervention
5 19 20 35 40.760 Intervention
5 19 20 36 44.753 Intervention
5 19 20 37 47.462 Intervention
5 19 20 38 48.816 Intervention
5 19 20 39 52.240 Intervention
5 19 20 40 50.288 Post-Intervention
5 19 20 41 51.077 Post-Intervention
5 19 20 42 48.519 Post-Intervention
5 19 20 43 51.011 Post-Intervention
5 19 20 44 49.589 Post-Intervention
5 19 20 45 50.637 Post-Intervention
5 19 20 46 47.937 Post-Intervention
5 19 20 47 45.461 Post-Intervention
5 19 20 48 52.178 Post-Intervention
5 19 20 49 48.010 Post-Intervention
5 19 20 50 47.008 Post-Intervention
5 19 20 51 50.800 Post-Intervention
5 19 20 52 47.707 Post-Intervention
5 19 20 53 54.898 Post-Intervention
5 19 20 54 49.207 Post-Intervention
5 19 20 55 52.968 Post-Intervention
5 19 20 56 51.581 Post-Intervention
5 19 20 57 47.745 Post-Intervention
5 19 20 58 53.559 Post-Intervention
5 19 20 59 48.210 Post-Intervention
5 19 20 60 50.766 Post-Intervention
6 24 20 1 13.027 Pre-Intervention
6 24 20 2 7.471 Pre-Intervention
6 24 20 3 13.680 Pre-Intervention
6 24 20 4 5.769 Pre-Intervention
6 24 20 5 10.789 Pre-Intervention
6 24 20 6 8.699 Pre-Intervention
6 24 20 7 9.323 Pre-Intervention
6 24 20 8 8.312 Pre-Intervention
6 24 20 9 12.865 Pre-Intervention
6 24 20 10 9.967 Pre-Intervention
6 24 20 11 6.926 Pre-Intervention
6 24 20 12 7.546 Pre-Intervention
6 24 20 13 10.175 Pre-Intervention
6 24 20 14 12.912 Pre-Intervention
6 24 20 15 5.829 Pre-Intervention
6 24 20 16 8.554 Pre-Intervention
6 24 20 17 10.514 Pre-Intervention
6 24 20 18 12.225 Pre-Intervention
6 24 20 19 9.299 Pre-Intervention
6 24 20 20 9.090 Pre-Intervention
6 24 20 21 9.931 Pre-Intervention
6 24 20 22 10.490 Pre-Intervention
6 24 20 23 12.348 Pre-Intervention
6 24 20 24 11.752 Pre-Intervention
6 24 20 25 10.447 Intervention
6 24 20 26 13.391 Intervention
6 24 20 27 16.231 Intervention
6 24 20 28 16.951 Intervention
6 24 20 29 21.799 Intervention
6 24 20 30 21.528 Intervention
6 24 20 31 21.879 Intervention
6 24 20 32 25.989 Intervention
6 24 20 33 26.665 Intervention
6 24 20 34 32.069 Intervention
6 24 20 35 32.795 Intervention
6 24 20 36 35.019 Intervention
6 24 20 37 37.480 Intervention
6 24 20 38 32.867 Intervention
6 24 20 39 35.365 Intervention
6 24 20 40 41.401 Intervention
6 24 20 41 46.800 Intervention
6 24 20 42 49.059 Intervention
6 24 20 43 46.944 Intervention
6 24 20 44 50.266 Intervention
6 24 20 45 54.224 Post-Intervention
6 24 20 46 50.032 Post-Intervention
6 24 20 47 51.803 Post-Intervention
6 24 20 48 49.742 Post-Intervention
6 24 20 49 48.780 Post-Intervention
6 24 20 50 48.083 Post-Intervention
6 24 20 51 48.620 Post-Intervention
6 24 20 52 48.069 Post-Intervention
6 24 20 53 48.062 Post-Intervention
6 24 20 54 46.871 Post-Intervention
6 24 20 55 50.262 Post-Intervention
6 24 20 56 44.337 Post-Intervention
6 24 20 57 51.324 Post-Intervention
6 24 20 58 48.036 Post-Intervention
6 24 20 59 54.790 Post-Intervention
6 24 20 60 48.737 Post-Intervention
7 10 19 1 9.695 Pre-Intervention
7 10 19 2 9.174 Pre-Intervention
7 10 19 3 10.637 Pre-Intervention
7 10 19 4 9.136 Pre-Intervention
7 10 19 5 7.362 Pre-Intervention
7 10 19 6 14.267 Pre-Intervention
7 10 19 7 9.448 Pre-Intervention
7 10 19 8 12.378 Pre-Intervention
7 10 19 9 6.093 Pre-Intervention
7 10 19 10 5.854 Pre-Intervention
7 10 19 11 9.539 Intervention
7 10 19 12 14.262 Intervention
7 10 19 13 13.727 Intervention
7 10 19 14 19.425 Intervention
7 10 19 15 20.669 Intervention
7 10 19 16 22.576 Intervention
7 10 19 17 26.741 Intervention
7 10 19 18 24.729 Intervention
7 10 19 19 28.852 Intervention
7 10 19 20 30.760 Intervention
7 10 19 21 31.628 Intervention
7 10 19 22 34.361 Intervention
7 10 19 23 36.095 Intervention
7 10 19 24 40.918 Intervention
7 10 19 25 39.432 Intervention
7 10 19 26 41.137 Intervention
7 10 19 27 43.049 Intervention
7 10 19 28 46.761 Intervention
7 10 19 29 46.199 Intervention
7 10 19 30 48.769 Post-Intervention
7 10 19 31 47.367 Post-Intervention
7 10 19 32 49.477 Post-Intervention
7 10 19 33 52.471 Post-Intervention
7 10 19 34 49.366 Post-Intervention
7 10 19 35 44.544 Post-Intervention
7 10 19 36 48.075 Post-Intervention
7 10 19 37 46.384 Post-Intervention
7 10 19 38 47.661 Post-Intervention
7 10 19 39 45.355 Post-Intervention
7 10 19 40 49.098 Post-Intervention
7 10 19 41 48.016 Post-Intervention
7 10 19 42 47.499 Post-Intervention
7 10 19 43 49.154 Post-Intervention
7 10 19 44 48.998 Post-Intervention
7 10 19 45 47.308 Post-Intervention
7 10 19 46 46.442 Post-Intervention
7 10 19 47 49.660 Post-Intervention
7 10 19 48 48.067 Post-Intervention
7 10 19 49 46.288 Post-Intervention
7 10 19 50 45.444 Post-Intervention
7 10 19 51 48.753 Post-Intervention
7 10 19 52 49.891 Post-Intervention
7 10 19 53 49.418 Post-Intervention
7 10 19 54 47.998 Post-Intervention
7 10 19 55 50.407 Post-Intervention
7 10 19 56 46.834 Post-Intervention
7 10 19 57 46.451 Post-Intervention
7 10 19 58 48.048 Post-Intervention
7 10 19 59 48.951 Post-Intervention
7 10 19 60 52.757 Post-Intervention
8 13 20 1 10.596 Pre-Intervention
8 13 20 2 11.746 Pre-Intervention
8 13 20 3 10.903 Pre-Intervention
8 13 20 4 11.403 Pre-Intervention
8 13 20 5 8.263 Pre-Intervention
8 13 20 6 7.589 Pre-Intervention
8 13 20 7 8.232 Pre-Intervention
8 13 20 8 7.295 Pre-Intervention
8 13 20 9 10.028 Pre-Intervention
8 13 20 10 10.105 Pre-Intervention
8 13 20 11 9.797 Pre-Intervention
8 13 20 12 11.889 Pre-Intervention
8 13 20 13 7.041 Pre-Intervention
8 13 20 14 12.705 Intervention
8 13 20 15 13.626 Intervention
8 13 20 16 14.990 Intervention
8 13 20 17 19.614 Intervention
8 13 20 18 23.575 Intervention
8 13 20 19 22.594 Intervention
8 13 20 20 25.971 Intervention
8 13 20 21 26.860 Intervention
8 13 20 22 30.064 Intervention
8 13 20 23 29.586 Intervention
8 13 20 24 31.227 Intervention
8 13 20 25 34.261 Intervention
8 13 20 26 36.188 Intervention
8 13 20 27 38.330 Intervention
8 13 20 28 42.613 Intervention
8 13 20 29 43.275 Intervention
8 13 20 30 42.504 Intervention
8 13 20 31 42.509 Intervention
8 13 20 32 51.437 Intervention
8 13 20 33 53.725 Intervention
8 13 20 34 50.306 Post-Intervention
8 13 20 35 48.788 Post-Intervention
8 13 20 36 51.408 Post-Intervention
8 13 20 37 53.231 Post-Intervention
8 13 20 38 50.961 Post-Intervention
8 13 20 39 53.231 Post-Intervention
8 13 20 40 48.398 Post-Intervention
8 13 20 41 48.722 Post-Intervention
8 13 20 42 50.481 Post-Intervention
8 13 20 43 51.476 Post-Intervention
8 13 20 44 50.614 Post-Intervention
8 13 20 45 48.026 Post-Intervention
8 13 20 46 50.541 Post-Intervention
8 13 20 47 50.111 Post-Intervention
8 13 20 48 49.685 Post-Intervention
8 13 20 49 50.818 Post-Intervention
8 13 20 50 45.583 Post-Intervention
8 13 20 51 50.140 Post-Intervention
8 13 20 52 49.661 Post-Intervention
8 13 20 53 47.786 Post-Intervention
8 13 20 54 49.989 Post-Intervention
8 13 20 55 52.467 Post-Intervention
8 13 20 56 50.677 Post-Intervention
8 13 20 57 49.147 Post-Intervention
8 13 20 58 51.386 Post-Intervention
8 13 20 59 47.671 Post-Intervention
8 13 20 60 50.322 Post-Intervention

Plot the results:

Plotting is rarely done all at once. We usually build it step-by-step and layer-by-layer. Here is the first attempt:

d_longer %>% 
  ggplot(aes(time, on_task)) +
  geom_line(aes(group = id, color = phase))

That was not so great! Let’s make a separate plot for each person:

d_longer %>% 
  ggplot(aes(time, on_task)) +
  geom_line(aes(group = id, color = phase)) + 
  facet_grid(rows = vars(id))

Better! Let’s reorder the cases by the order in which the intervention is first implemented:

d_longer %>% 
  mutate(id = fct_reorder(factor(id), baseline_sessions_n)) %>% 
  ggplot(aes(time, on_task)) +
  geom_line(aes(group = id, color = phase)) + 
  facet_grid(rows = vars(id))

Looks even better!

If I wanted a more polished plot for publication, I might create something like Figure 1.

Code
# Some pre-processing for easier plotting
d_longer_processed <- d_longer %>% 
    mutate(id = factor(id) %>% fct_reorder(baseline_sessions_n),
         phase = fct_inorder(phase)) %>% 
  arrange(phase) %>% 
  mutate(id = factor(id, labels = LETTERS[1:8]))

# data for phase rectangles
d_phases <- d_longer_processed %>% 
  summarise(.by = c(id, phase),
            begin = min(time),
            end = max(time)) %>% 
  mutate(begin = ifelse(begin == 1, 0, begin),
         end = end + 1)

d_longer_processed %>%
  ggplot(aes(time, on_task)) +
    geom_vline(xintercept = rep(1:4, 12) + rep(seq(0,55,5), each = 4), linewidth = unit(.1, "mm"), color = "gray80") +
  ggtext::geom_richtext(
    data = . %>% filter(id == "E", time %in% c(10, 30, 50)),
    aes(label = phase, color = phase),
    label.color = NA,
    fill = scales::alpha("white", .5),
    label.padding = unit(0, "mm"),
    label.margin = unit(2, "mm"),
    vjust = c(0, 0, 1),
    angle = c(0, 21, 0),
    size = 5.5,
    family = "Roboto Condensed"
  ) +
  geom_rect(
    data = d_phases,
    aes(
      xmin = begin,
      xmax = end,
      ymin = 0,
      ymax = 60,
      fill = phase
    ),
    inherit.aes = FALSE
  ) +
  geom_line(aes(group = id, color = phase), linewidth = 1) +
  facet_grid(rows = vars(id)) +
  theme(strip.text.y = element_text(angle = 0),
        legend.position = "none") +
  labs(x = "Day", y = "Time on Task")  +
  theme_light(base_family = "Roboto Condensed", base_size = 18) +
  theme(legend.position = "none",
        panel.border = element_blank(),
        strip.text.y = element_text(angle = 0), 
        axis.text.y = element_text(vjust = c(0))) +
  scale_color_viridis_d(alpha = .9, begin = .1, end = .7) +
  scale_fill_viridis_d(alpha = .15, begin = .1, end = .7) +
  scale_y_continuous(
    limits = c(0, 60),
    breaks = seq(0, 50, 10),
    expand = expansion(0)
  ) +
  scale_x_continuous(limits = c(0, 61), breaks = seq(0, 60, 10), minor_breaks = seq(0,60, 5), expand = expansion(0)) +
   guides(
    x = guide_axis(minor.ticks = TRUE)
    # y = guide_axis(minor.ticks = TRUE)
  ) + 
  coord_cartesian(clip = "off")
Figure 1: Effect on Intervention on Time on Task for Eight Children