[英]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) 第二个数据框反映了层次结构的命名结构(例如,A是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. 如果x中缺少一个值,我想用fill的上级填充它。 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 我们可以基于NA
元素创建逻辑向量('i1'),获取具有match
'filler'中匹配元素的索引,然后进行赋值
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. 由于x
是命名向量,我们可以将其转换为数据帧( enframe
),然后进行enframe
,将NA
值替换为对应的value
并在需要时再次将其转换为向量。 ( deframe
). ( 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.