Replacing NA values in a vector with a sequence
Asked Answered
A

5

5

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?

Adjudicate answered 5/11, 2022 at 15:11 Comment(0)
D
4

Using replace.

f <- \(x) replace(x, is.na(x), sprintf('x%03d', seq_len(sum(is.na(x)))))

f(avec)
# [1] "somevar"    "x001"       "anothervar" "x002"       "thisvar"    "thatvar"    "x003"      
# [8] "lastvar"    "x004"    
Demoralize answered 5/11, 2022 at 15:21 Comment(0)
J
3

One solution:

avec[is.na(avec)] = paste0("x00", seq_along(avec[is.na(avec)]))

[1] "somevar"    "x001"       "anothervar" "x002"       "thisvar"    "thatvar"    "x003"       "lastvar"    "x004"
Jeremiad answered 5/11, 2022 at 15:25 Comment(1)
avec[is.na(avec)] <- paste0("x", formatC(seq_len(sum(is.na(avec))), 2, flag = "0")) for more flexibility in the numbersLacour
B
3

Another option:

dplyr::coalesce(avec, sprintf("X%03i", cumsum(is.na(avec))))
#> [1] "somevar"    "X001"       "anothervar" "X002"       "thisvar"   
#> [6] "thatvar"    "X003"       "lastvar"    "X004"
Barncard answered 5/11, 2022 at 15:31 Comment(0)
C
3

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"  
Contemplation answered 5/11, 2022 at 15:34 Comment(0)
C
2

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("X%03i", na_pos)
    na_pos <- na_pos + 1
  }
}
avec

# [1] "somevar"    "X001"       "anothervar" "X002"       "thisvar"    "thatvar"    "X003"       "lastvar"    "X004"      
Chinachinaberry answered 5/11, 2022 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.