简体   繁体   中英

Conditionally fill empty cells

I have a named vector with some missing values:

x = c(99, 88, 1, 2, 3, NA, NA)
names(x) = c("A", "C", "AA", "AB", "AC", "AD", "CA")

And a second dataframe which reflects the hierarchical naming structure (eg A is a superordinate to AA, AB, & AC)

filler = data.frame(super = c("A", "A", "A", "A", "C"), sub = c("AA", "AB", "AC", "AD", "CA"))

If a value is missing in x, I want to fill it with the superordinate from filler. So that the outcome would be

x = c(99, 88, 1, 2, 3, 99, 88)

Does anyone have any clever way to do this without looping through each possibility?

We can create a logical vector ('i1') based on the NA elements, get the index of matching elements in 'filler' with match and then do the assignmnt

i1 <- is.na(x)
x[i1] <- x[match(filler$super[match(names(x[i1]), filler$sub)], names(x))] 
as.vector(x)
#[1] 99 88  1  2  3 99 88

As x is a named vector we could convert it to a dataframe ( enframe ) and then do a join, replace NA values with corresponding value and if needed convert it into vector again. ( deframe ).

library(dplyr)
library(tibble)

enframe(x) %>%
  left_join(filler, by = c("name" = "sub")) %>%
   mutate(value = if_else(is.na(value), value[match(super, name)], value)) %>%
   select(-super) %>%
   deframe()

# A  C AA AB AC AD CA 
#99 88  1  2  3 99 88 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM