Вот один вариант использования map2_dbl
из purrr
. Мы group_by
treatment
и заменим NA
temp первым не-NA temp
тем же date
в группе.
library(dplyr)
library(purrr)
df %>%
group_by(treatment) %>%
mutate(temp = map2_dbl(temp, date, ~if (is.na(.x))
temp[which.max(date == .y & !is.na(temp))] else .x))
# date treatment sensor temp
# <fct> <int> <fct> <dbl>
# 1 1/01/2019 1 A 30
# 2 2/01/2019 1 A 29.1
# 3 3/01/2019 1 A 21.2
# 4 4/01/2019 1 A 23.5
# 5 1/01/2019 1 B 20.5
# 6 2/01/2019 1 B 19.8
# 7 3/01/2019 1 B 35.1
# 8 4/01/2019 1 B 23.5
# 9 1/01/2019 2 C 31.2
#10 2/01/2019 2 C 32.1
#11 3/01/2019 2 C 28.1
#12 4/01/2019 2 C 31.2
#13 1/01/2019 2 D 31.2
#14 2/01/2019 2 D 26.5
#15 3/01/2019 2 D 27.9
#16 4/01/2019 2 D 28
данные
df <- structure(list(date = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("1/01/2019",
"2/01/2019", "3/01/2019", "4/01/2019"), class = "factor"), treatment =
c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
sensor = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("A", "B", "C", "D"
), class = "factor"), temp = c(30, 29.1, 21.2, NA, 20.5,
19.8, 35.1, 23.5, 31.2, 32.1, 28.1, 31.2, NA, 26.5, 27.9,
28)), class = "data.frame", row.names = c(NA, -16L))