I have data as follows:
avec <- c("somevar", NA ,"anothervar", NA, "thisvar","thatvar", NA, "lastvar", NA )
All I want to do is to replace all NA
values in avec
, with a consecutive variable name, like x001
to x00n
. I thought that this would be very easy but I could not find anything on stack.
Desired output:
avec <- c("somevar", "x001","anothervar", "x002", "thisvar","thatvar", "x003", "lastvar", "x004")
How should I do this?
avec[is.na(avec)] <- paste0("x", formatC(seq_len(sum(is.na(avec))), 2, flag = "0"))
for more flexibility in the numbers – Lacour