简体   繁体   English

如何用另一个变量中缺失值的累积和填充数据框

[英]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.

相关问题 R 数据框 - 用另一列的条件填充缺失值 - R data frame - fill missing values with condition on another column 如何通过匹配另一个数据框来填充数据框列值? - How to fill in data frame column values by matching another data frame? R:如何填充数据框中缺失的年份值 - R: How to fill up missing year values in a data frame R中data.frame的每个变量的累积和? - Cumulative sum of each each variable of a data.frame in R? 如何计算考虑第一个数据帧中的一个变量的百分比是来自另一个数据帧中不同值的总和 - How to calculate percentages considering one variable in first dataframe is an aggregated sum from different values in another data frame 如何创建一个新变量,该变量是另一个二进制变量的条件累加和? - How to create a new variable that is a conditional cumulative sum of another binary variable? 从另一个数据框填充一个数据框的值 - Fill in values of a data frame from another data frame 如何根据字符变量中的唯一值获取累计和? - How to take the cumulative sum based on unique values in a character variable? 如何通过唯一值累加和变量并输入回 - how to cumulative sum variable by unique values and input back in 从数据框中心开始的累积总和-R - Cumulative Sum Starting at Center of Data Frame - R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM