[英]How to fill a data frame with cumulative sum with missing values in another variable
Imagine you have the following data frame假设您有以下数据框
x<- c(3, 5, 9, 12, 14)
y<- c(0.2, 0.4, 0.7, 1.4, 1.8)
df<- data.frame(x, y)
df
I asked a few months ago, how to fill "x" with remaining numbers and those numbers take the value zero in "y".几个月前我问过,如何用剩余的数字填充“x”,而这些数字在“y”中取零值。 And the answer was:
答案是:
df <- tidyr::complete(df, x = 0:16, fill = list(y = 0))
cbind(df$x, df$y)
Now, I'd like to fill the numbers in the following way, but automatically, and I don't know if it is possible.现在,我想按以下方式填写数字,但自动填写,我不知道是否可能。
How to obtein "y1" automatically如何自动获取“y1”
Thanks in advance.提前致谢。
df$y1<- c(0,0,0, 0.2,0.2, 0.4,0.4,0.4,0.4, 0.7,0.7,0.7, 1.4,1.4, 1.8,1.8,1.8)
cbind(df$x, df$y1)
Instead of specifying the fill
in complete
, leave it as it is, so that by default it gets filled by NA
, then use fill
from tidyr
to update the NA elements with the previous non-NA而不是在
complete
中指定fill
,而是保持原样,以便默认情况下它由NA
填充,然后使用tidyr
中的fill
用先前的非 NA 更新 NA 元素
library(dplyr)
library(tidyr)
tidyr::complete(df, x = 0:16) %>%
fill(y, .direction = "down") %>%
mutate(y = replace(y, is.na(y), 0))
-output -输出
# A tibble: 17 × 2
x y
<dbl> <dbl>
1 0 0
2 1 0
3 2 0
4 3 0.2
5 4 0.2
6 5 0.4
7 6 0.4
8 7 0.4
9 8 0.4
10 9 0.7
11 10 0.7
12 11 0.7
13 12 1.4
14 13 1.4
15 14 1.8
16 15 1.8
17 16 1.8
df %>%
complete(x=0:16) %>%
fill(y) %>%
replace_na(list(y=0))
# A tibble: 17 x 2
x y
<dbl> <dbl>
1 0 0
2 1 0
3 2 0
4 3 0.2
5 4 0.2
6 5 0.4
7 6 0.4
8 7 0.4
9 8 0.4
10 9 0.7
11 10 0.7
12 11 0.7
13 12 1.4
14 13 1.4
15 14 1.8
16 15 1.8
17 16 1.8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.