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?
CodePudding user response:
Using replace
.
f <- \(x) replace(x, is.na(x), sprintf('xd', seq_len(sum(is.na(x)))))
f(avec)
# [1] "somevar" "x001" "anothervar" "x002" "thisvar" "thatvar" "x003"
# [8] "lastvar" "x004"
CodePudding user response:
One solution:
avec[is.na(avec)] = paste0("x00", seq_along(avec[is.na(avec)]))
[1] "somevar" "x001" "anothervar" "x002" "thisvar" "thatvar" "x003" "lastvar" "x004"
CodePudding user response:
Another option:
dplyr::coalesce(avec, sprintf("Xi", cumsum(is.na(avec))))
#> [1] "somevar" "X001" "anothervar" "X002" "thisvar"
#> [6] "thatvar" "X003" "lastvar" "X004"
CodePudding user response:
I can better think in terms of columns for this, here is my approach:
library(dplyr)
as.data.frame(avec) %>%
mutate(avec = ifelse(is.na(avec), paste0("x00", cumsum(is.na(avec))), avec)) %>%
pull(avec)
[1] "somevar" "x001" "anothervar" "x002" "thisvar" "thatvar" "x003" "lastvar"
[9] "x004"
CodePudding user response:
avec <- c("somevar", NA ,"anothervar", NA, "thisvar","thatvar", NA, "lastvar", NA )
na_pos <- 1
for (i in avec |> length() |> seq()) {
if (is.na(avec[[i]])) {
avec[[i]] <- sprintf("Xi", na_pos)
na_pos <- na_pos 1
}
}
avec
# [1] "somevar" "X001" "anothervar" "X002" "thisvar" "thatvar" "X003" "lastvar" "X004"