如何填充数据直到最后一个非缺失值?
How do I fill data until last non-missing value?
我有一些数据是这样分组的:
events <- structure(list(let = c("A", "A", "A", "B", "B", "B"), age = c(0L,
4L, 16L, 0L, 8L, 7L), value = c(61L, 60L, 13L, 29L, 56L, 99L)),
class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))
let age value
1 A 0 61
2 A 4 60
3 A 16 13
4 B 0 29
5 B 8 56
6 B 7 99
如何转换数据框以便:
- 年龄是按周分组的多个列。所以对于每一列,取小于等于0、7、14等最大年龄的值days
- 填写年龄 UNTIL 最后一个非缺失值
let
。
最终结果如下所示:
events.cast <- data.frame(
let = LETTERS[1:2],
T0_value = c(61,29),
T1_value = c(60,99),
T2_value = c(60,56),
T3_value = c(13,56))
let T0_value T1_value T2_value T3_value
1 A 61 60 60 13
2 B 29 99 56 NA
请注意,这是来自 我问的。
我们可以在 complete
之前创建一个 'actuals' 列,并根据 NA
的出现使用它在 'value' 列中创建 NA
] 在 'actuals'
library(dplyr)
library(tidyr)
library(stringr)
events %>%
group_by(grp = cut(age, breaks = c(-Inf,0, 7, 14, 21),
labels = str_c("T", 0:3, "_value")), let) %>%
slice(which.max(value)) %>%
ungroup %>%
select(-age) %>%
mutate(actuals = TRUE) %>%
group_by(let) %>%
complete(grp = unique(.$grp)) %>%
fill(value) %>%
ungroup %>%
mutate(i1 = cumsum(is.na(actuals)),
value = replace(value, i1 == max(i1), NA)) %>%
select(-i1, -actuals) %>%
pivot_wider(names_from = grp, values_from = value)
# A tibble: 2 x 5
# let T0_value T1_value T2_value T3_value
# <chr> <int> <int> <int> <int>
#1 A 61 60 60 13
#2 B 29 99 56 NA
我有一些数据是这样分组的:
events <- structure(list(let = c("A", "A", "A", "B", "B", "B"), age = c(0L,
4L, 16L, 0L, 8L, 7L), value = c(61L, 60L, 13L, 29L, 56L, 99L)),
class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))
let age value
1 A 0 61
2 A 4 60
3 A 16 13
4 B 0 29
5 B 8 56
6 B 7 99
如何转换数据框以便:
- 年龄是按周分组的多个列。所以对于每一列,取小于等于0、7、14等最大年龄的值days
- 填写年龄 UNTIL 最后一个非缺失值
let
。
最终结果如下所示:
events.cast <- data.frame(
let = LETTERS[1:2],
T0_value = c(61,29),
T1_value = c(60,99),
T2_value = c(60,56),
T3_value = c(13,56))
let T0_value T1_value T2_value T3_value
1 A 61 60 60 13
2 B 29 99 56 NA
请注意,这是来自
我们可以在 complete
之前创建一个 'actuals' 列,并根据 NA
的出现使用它在 'value' 列中创建 NA
] 在 'actuals'
library(dplyr)
library(tidyr)
library(stringr)
events %>%
group_by(grp = cut(age, breaks = c(-Inf,0, 7, 14, 21),
labels = str_c("T", 0:3, "_value")), let) %>%
slice(which.max(value)) %>%
ungroup %>%
select(-age) %>%
mutate(actuals = TRUE) %>%
group_by(let) %>%
complete(grp = unique(.$grp)) %>%
fill(value) %>%
ungroup %>%
mutate(i1 = cumsum(is.na(actuals)),
value = replace(value, i1 == max(i1), NA)) %>%
select(-i1, -actuals) %>%
pivot_wider(names_from = grp, values_from = value)
# A tibble: 2 x 5
# let T0_value T1_value T2_value T3_value
# <chr> <int> <int> <int> <int>
#1 A 61 60 60 13
#2 B 29 99 56 NA