Мы можем использовать spread
после mutate
в столбце 'time'
library(tidyverse)
df1 %>%
mutate(time = str_c("time", time -1)) %>%
spread(time, residuals)
# id time1 time2
#1 1 -2.0 2.1
#2 2 -1.3 2.0
#3 3 -1.4 1.5
данные
df1 <- structure(list(id = c(1L, 1L, 2L, 2L, 3L, 3L), time = c(2L, 3L,
2L, 3L, 2L, 3L), residuals = c(-2, 2.1, -1.3, 2, -1.4, 1.5)),
class = "data.frame", row.names = c(NA,
-6L))