Вы можете mutate
новый столбец с оператором ifelse
, чтобы проверить значение NA
:
library(dplyr)
df <- structure(list(ID = c(762L, 762L, 763L, 763L, 763L), Value = c(2L,
2L, NA, 8L, 9L)), class = "data.frame", row.names = c(NA, -5L
))
df_filled <- df %>% mutate(value_filled = ifelse(is.na(.$Value),as.integer(substr(as.character(.$ID),1,1)),.$Value))