1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
library(tidyverse)
remplacer_NA <- function(x,y){
l=str_replace_all(x,"NA","N")
idx1=str_locate(l,"1")[1,"start"]
idxNA=y
if(idx1<idxNA){return("p")}else{"0"}
}
df=read.csv2("le_df.csv") |>mutate(across(starts_with("M"),as.character)) |>
unite("ms",M1:M6,sep="",remove = FALSE) |>
pivot_longer(-c(ID,ms),names_to = "mois",values_to = "val") |>
group_by(ID) |> mutate(rang=row_number()) |> rowwise() |>
mutate(val=ifelse(is.na(val),remplacer_NA(ms,rang),val)) |>
select(-ms,-rang) |>
pivot_wider(names_from = mois,values_from = val) |
Partager