我有以下数据
library(tidyr)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(data.table)
#>
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#>
#> between, first, last
df <- structure(list(filename = c("PS92_019-6_rovT_irrad.tab", "PS92_019-6_rovT_irrad.tab",
"PS92_019-6_rovT_irrad.tab", "PS92_019-6_rovT_irrad.tab"), depth = c(5,
10, 20, 75), ps = c(3.26223404971255, 3.38947945477306, 3.97380593851983,
0.428074807655144)), row.names = c(NA, -4L), class = c("tbl_df", "tbl",
"data.frame"), .Names = c("filename", "depth", "ps"))
df
#> # A tibble: 4 x 3
#> filename depth ps
#> <chr> <dbl> <dbl>
#> 1 PS92_019-6_rovT_irrad.tab 5 3.2622340
#> 2 PS92_019-6_rovT_irrad.tab 10 3.3894795
#> 3 PS92_019-6_rovT_irrad.tab 20 3.9738059
#> 4 PS92_019-6_rovT_irrad.tab 75 0.4280748
在此数据中,在深度 = 0 处缺少观测值。使用 tidyr,
我可以通过以下方式完成它:
df %>% tidyr::complete(depth = c(0, unique(depth))) %>% fill(everything(), .direction = "up") ## use the last observations to fill the new line
#> # A tibble: 5 x 3
#> depth filename ps
#> <dbl> <chr> <dbl>
#> 1 0 PS92_019-6_rovT_irrad.tab 3.2622340
#> 2 5 PS92_019-6_rovT_irrad.tab 3.2622340
#> 3 10 PS92_019-6_rovT_irrad.tab 3.3894795
#> 4 20 PS92_019-6_rovT_irrad.tab 3.9738059
#> 5 75 PS92_019-6_rovT_irrad.tab 0.4280748
问题是我必须在大型数据集上运行它,我发现
完成/填充功能有点慢。因此,我想给
与 data.table 一起看看它是否可以加快速度。但是,我
我无法理解它。任何帮助表示赞赏。