R: Long and Wide Format for Repeated Measures

Leerer Labortisch mit zwei verschiedenen Anordnungen von Probenröhrchen

A repeated measurement per person initially sounds straightforward: the same questionnaire at three time points, the same laboratory sample under several conditions, or the same performance before and after an intervention. For the analysis, however, the data structure helps determine whether the R code remains correct, comprehensible and extensible. The Long and Wide Format for Repeated Measures in R does not describe two competing file formats, but two representations of the same information - with different strengths for analysis, visualisation and data checking.

Why the data format affects the analysis

With repeated measures, several rows or values belong to the same unit of analysis. This unit can be a person, an animal, a business or a sample. The observations are therefore not independent of one another. It is precisely this dependency that the statistical model must take into account.

Whether a repeated-measures ANOVA, a linear mixed model or a graphical representation can be implemented sensibly also depends on how time points and measurements are stored. Many problems do not arise in the model itself, but earlier: an ID is missing, time points are named inconsistently, individual values were imported incorrectly, or covariates are not assigned unambiguously.

The central rule is: each row should represent a clearly defined observation unit. In Long format, this is usually a single measurement of a person at a time point or under a condition. This logic fits particularly well with modern R workflows.

Wide format: one case per row

In Wide format, each unit of analysis appears in exactly one row. The repeated measurements are spread across several columns. For a study with three measurement time points, a data set could look like this:

| id | gruppe | stress_t1 | stress_t2 | stress_t3 |
|---|---|---:|---:|---:|
| 101 | Intervention | 24 | 18 | 15 |
| 102 | Control | 20 | 21 | 19 |

The variables stress_t1, stress_t2 and stress_t3 contain the same outcome, but at different time points. This format is often intuitive for data entry and an initial plausibility check. Simple change scores can also be formed directly, for example stress_t3 - stress_t1.

Wide data can be useful when there are only a few, fixed time points and software or a specific analysis command expects this format. In R, for example, some classical procedures can also be implemented with wide data. For more complex questions, however, the structure quickly becomes unwieldy.

As soon as additional conditions, multiple outcomes or differing numbers of measurements per person are added, the number of columns grows rapidly. Names such as wert_t2_bedingungB_followup are then technically possible, but error-prone and difficult to document. Missing values can also be checked less transparently: is a value missing only at one time point, or is the entire person missing? In Wide format, this distinction is not always immediately visible.

Long format: one measurement per row

In Long format, repeated measurements of the same person appear in several rows. The measurement time points or conditions are stored in a separate variable:

| id | gruppe | zeitpunkt | stress |
|---|---|---|---:|
| 101 | Intervention | t1 | 24 |
| 101 | Intervention | t2 | 18 |
| 101 | Intervention | t3 | 15 |
| 102 | Kontrolle | t1 | 20 |
| 102 | Kontrolle | t2 | 21 |
| 102 | Kontrolle | t3 | 19 |

The id column links the measurements of the same person. zeitpunkt describes when the measurement was taken, and stress contains the observed value. Person-specific characteristics such as group, age or sex are repeated for all associated rows. This is not unnecessary duplication, but the necessary structure so that R recognises the relationship between measurement, person and condition.

For linear mixed models, this format is practically the standard. A model can, for example, include a fixed effect of time point and group, as well as a random intercept per person. This takes account of the fact that people have different starting levels. With suitable data, mixed models can also handle incomplete measurement series without automatically excluding people from the entire analysis. However, this requires the assumptions about the missing data mechanism to be examined substantively.

Long or wide format for repeated measures?

There is no blanket rule that Long is always better. The decisive factors are the analysis objective, the software and the study design. Wide data are well suited as a clear source file or for calculations that directly compare measurement time points. Long data are usually the better working format for visualisations, data checks and model-based analyses with R.

The advantage is particularly clear in a study with two groups and four time points. In Long format, gruppe can be modelled as a between-subject factor and zeitpunkt as a within-subject factor. The interaction of the two variables then answers the substantive question of whether the change over time differs between groups. In Wide format, this structure would first have to be derived from several columns.

Long is often advantageous when there are several dependent variables as well. Here there are two possible levels: a variable such as outcome can denote the type of measurement, while wert contains the numeric value. Whether this further reshaping makes sense depends on whether the outcomes are to be processed together or modelled separately. An overly condensed structure can also make analyses unnecessarily complicated.

Safely reshaping data in R

If a file is available in Wide format, it can be converted to Long format with pivot_longer() from the tidyr package. Suppose all measurement variables begin with stress_:

library(dplyr)
library(tidyr)

daten_long <- daten_wide |>
  pivot_longer(
    cols = starts_with("stress_"),
    names_to = "zeitpunkt",
    values_to = "stress"
  ) |>
  mutate(
    zeitpunkt = sub("stress_", "", zeitpunkt),
    zeitpunkt = factor(zeitpunkt, levels = c("t1", "t2", "t3"))
  )

The code gathers the columns stress_t1 to stress_t3 into two new variables. The explicit order of the factor levels is important. Without this specification, R may sort time points alphabetically. With labels such as t1, t2 and t10, this easily leads to an incorrect order in tables and graphs.

The reverse route back to Wide format is achieved with pivot_wider(). This is useful, for example, when you want to calculate change scores or need an output for a specific procedure:

daten_wide_neu <- daten_long |>
  pivot_wider(
    names_from = zeitpunkt,
    values_from = stress,
    names_prefix = "stress_"
  )

Before any reshaping, you should check whether the combination of id and zeitpunkt is unique. If there are several rows for the same person and the same time point, this requires a substantive explanation - for example, several technical repetitions. These must not be treated unnoticed as independent person measurements. Where necessary, they must first be aggregated or considered as additional nesting in the model.

Typical sources of error before modelling

A common mistake is to use a row number as the person ID. After reshaping, each measurement has its own row number; it cannot represent which measurements belong to a person. Use a stable, pseudonymised ID from data collection instead.

The coding of the time variable is also relevant. If the intervals between measurement times are equal and only the comparison of the appointments is of interest, zeitpunkt can be modelled as a factor. With actual, possibly unequal time intervals, a numeric time variable may be more appropriate. Then one is not only comparing appointments, but estimating a time trend. Which variant is appropriate depends on the research question and the measurement plan.

Also ensure that group-related and person-related variables remain consistent. If a person is assigned to the intervention group in one row and to the control group in another row, this is usually a data error. Such contradictions are particularly easy to spot in Long format.

Missing values should not be replaced hastily by mean values. First, it should be clarified how many measurements are missing, at which time points this occurs and whether dropouts are associated with observable characteristics. A repeated-measures ANOVA and a mixed model handle missing data differently. The data structure facilitates implementation, but does not replace a methodological decision.

From structure to transparent reporting

Document in the methods section how the data were organised and analysed: Which ID identified the unit of analysis? Which time points or conditions were considered as a within-subject factor? How were missing measurements handled? In a mixed model, it should also be clear which fixed and random effects were specified.

A cleanly structured Long data set not only makes these details easier, but also increases reproducibility. The reshaping can be documented in the R script instead of moving data manually in tables. This keeps raw data, preparation and analysis clearly separate from one another.

If it is unclear which format suits your design or how repeated measures can be modelled correctly in R, individual statistical advice can help. Easy Statistik supports you in aligning data structure, model choice and presentation of results with your research question - discreetly and transparently via the contact form.


Put knowledge into practice

From explanation
to your own analysis.

Would you like to know which method suits your project? We review your plans free of charge and give you a clear recommendation.

Your question deserves a personal answer.

Describe your project. We will assess free of charge what support would be right for you.

This site is protected by hCaptcha and the hCaptcha Privacy Policy and Terms of Service apply.