按组(in R)向数据集添加新的空白行



我使用r,我有这样的数据框架:

dat <- data.frame(
group = c(1,1,1,1,1,1,2,2,2,2,2),
horizon  = c(1,3,5,6,7,10,1,3,5,9,10),
value = c(1.0,0.9,0.8,0.6,0.3,0.0,0.5,0.6,0.8,0.9,0.8)
other = c(a,a,a,a,a,a,b,b,b,b,b)
)

并且我想为每个缺失的地平线添加row(第一组为2,4,8和9,第二组为2,4,6,7,8)。缺失视界的值(value)将为空白。

我想要这样的东西:

datx <- data.frame(
group = c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2),
horizon  = c(1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10),
value = c(1.0,"na",0.9,"na",0.8,0.6,0.3,"na","na",0.0,0.5,"na",0.6,"na",0.8,"na","na","na",0.9,0.8)
other = c(a,a,a,a,a,a,a,a,a,a,b,b,b,b,b,b,b,b,b,b)
)

。具有新视野的扩展数据集,空白或"value"中的空格变量和保留的"其他";变量。

这只是一个例子。我实际上正在处理一个更大的数据集。

如果没有分组,问题将更容易解决,我将使用这样的内容:

newdat <- merge(data.frame(horizon=seq(1,10,1)),dat,all=TRUE)
newdat <- newdat[order(newdat$horizon),]

谢谢你的帮助!

我将假设变量other中的值是字符ab,并且这与变量group完全冗余。如果是这种情况,您可以使用dplyr包中的full_join来完成此操作。

a="a"
b="b"
dat <- data.frame(
group = c(1,1,1,1,1,1,2,2,2,2,2),
horizon  = c(1,3,5,6,7,10,1,3,5,9,10),
value = c(1.0,0.9,0.8,0.6,0.3,0.0,0.5,0.6,0.8,0.9,0.8),
other = c(a,a,a,a,a,a,b,b,b,b,b)
)
groups <- expand.grid(group=c(1,2),horizon=1:10)
groups <- groups %>% dplyr::mutate(other=ifelse(group==1,"a","b"))
dat %>%
dplyr::full_join(groups,by=c('group','horizon','other')) %>%
dplyr::arrange(group,horizon)

使用data.table:

library(data.table)
setDT(dat)
fill = c("other")
RES = 
dat[CJ(group = group, horizon = min(horizon):max(horizon), unique = TRUE), 
on = .(group, horizon)
][, (fill) := lapply(.SD, (x) x[which.min(is.na(x))]), by = group, .SDcols = fill]

RES[]
#     group horizon value  other
#     <num>   <int> <num> <char>
#  1:     1       1   1.0      a
#  2:     1       2    NA      a
#  3:     1       3   0.9      a
#  4:     1       4    NA      a
#  5:     1       5   0.8      a
#  6:     1       6   0.6      a
#  7:     1       7   0.3      a
#  8:     1       8    NA      a
#  9:     1       9    NA      a
# 10:     1      10   0.0      a
# 11:     2       1   0.5      b
# 12:     2       2    NA      b
# 13:     2       3   0.6      b
# 14:     2       4    NA      b
# 15:     2       5   0.8      b
# 16:     2       6    NA      b
# 17:     2       7    NA      b
# 18:     2       8    NA      b
# 19:     2       9   0.9      b
# 20:     2      10   0.8      b
#     group horizon value  other

最新更新