In my example:
library(data.table)
id <- 1:10
x <- rnorm(n=10,mean=10)
groups <- ifelse(x<5,"D","S")
df <- data.frame(id,x,groups)
str(df)
# 'data.frame': 10 obs. of 3 variables:
# $ id : int 1 2 3 4 5 6 7 8 9 10
# $ x : num 9.28 9.68 11.64 11.33 9.34 ...
# $ groups: chr "S" "S" "S" "S" ...
setDT(df)[,id:=rleid(groups)][groups=="D",decision:=fifelse(1:.N>=3, "Combate","Monitora"), by=id][,.(groups,decision)]
Error in
[.data.table(setDT(df)[,:=(id, rleid(groups))], groups == : Supplied 2 items to be assigned to group 1 of size 0 in column 'decision'. The RHS length must either be 1 (single values are ok) or match the LHS length exactly. If you wish to 'recycle' the RHS please use rep() explicitly to make this intent clear to readers of your code.
but I'd like as output if i don't have any D level:
# groups decision
# 1: S <NA>
# 2: S <NA>
# 3: S <NA>
# 4: S <NA>
# 5: S <NA>
# 6: S <NA>
# 7: S <NA>
# 8: S <NA>
# 9: S <NA>
# 10: S <NA>
Please, any help with it?