R 按顺序重复在NAs中插入值

R 按顺序重复在NAs中插入值,r,insert,na,R,Insert,Na,我有一个使我的数据完整的问题。下面是我的数据 > head(DF1) # A tibble: 6 x 4 Date Coalprice Gasprice Co2emissionprice <date> <dbl> <dbl> <dbl> 1 2015-12-31 47.45 14.40 8.22 2 2015-12-30 47.45

我有一个使我的数据完整的问题。下面是我的数据

> head(DF1)
# A tibble: 6 x 4
      Date     Coalprice Gasprice Co2emissionprice
    <date>     <dbl>    <dbl>            <dbl>
1 2015-12-31     47.45    14.40             8.22
2 2015-12-30     47.45    14.30             8.22
3 2015-12-29     47.40    15.40             8.27
4 2015-12-28     47.00    14.42             8.32
5 2015-12-25     47.00    14.20             8.22
6 2015-12-24     47.00    14.20             8.22
结构(列表日期=结构, 16794, 16793, 16792, 16791, 16790, 16787, 16786, 16785, 16784, 16783, 16780, 16779, 16778, 16777, 16776, 16773, 16772, 16771, 1677016769167661676516764167631676216759),class=“日期”), 煤价=c(47.45,47.45,47.4,47,47,47,47,47,47.6,47.6, 47.8, 47.75, 47.75, 47.7, 47.65, 47.35, 47.4, 47.45, 47.4, 47.75, 48.55, 48.95, 49.1, 49.7, 49.95, 50.3, 53.85, 53.95, 53.95,54,54.35),加斯普里斯=c(14.4,14.3,15.4,14.42, 14.2, 14.2, 13.93, 13.85, 14.35, 14.9, 15.5, 15.25, 15.95, 16.08, 16.23, 16.5, 16.65, 16.75, 16.78, 17.15, 17.15, 17.85, 17.95,18.2,17.7,17.7,17.88,17.7,17.6,17.5),二氧化碳排放价格=c(8.22, 8.22, 8.27, 8.32, 8.22, 8.22, 8.22, 8.25, 8.18, 8.07, 8.07, 8.12, 8.19, 8.09, 8.07, 8.36, 8.4, 8.42, 8.42, 8.52, 8.58, 8.49,8.55,8.58,8.56,8.58,8.62,8.65,8.56,8.51)),.Names=c(“日期”, “煤炭价格”、“天然气价格”、“二氧化碳排放价格”),row.names=c(NA,
-30L),class=c(“tbl_df”、“tbl”、“data.frame”)您可以使用
tidyr
包在一行中完成此操作

library(tidyr)

df <- fill(df, contains("price"), .direction = "down")
library(tidyr)

df应该通过粘贴它来工作吗?因为它不起作用。我就这样做了<代码>DF1我的道歉,我忘记了
处变异是不必要的。没有必要道歉……)那你能再给我一次启发吗?非常感谢更新了我的答案。我应用了语法,没有错误,但也没有什么不同。我还有空的周末。。
library(tidyr)

df <- fill(df, contains("price"), .direction = "down")