来自tidyr的完整/填充的data.table等效项 [英] data.table equivalent of complete/fill from tidyr

查看:78
本文介绍了来自tidyr的完整/填充的data.table等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据

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,
我可以用以下命令完成它:

In this data, there is a missing observation at depth = 0. Using tidyr, I can complete it with:

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来看看它是否可以加快速度。但是,我
无法解决这个问题。任何帮助表示赞赏。

The problem is that I have to run this on a large dataset and I found that the complete/fill functions are a bit slow. Thus, I wanted to give it a go with data.table to see if it can speed up things. However I can't get my head around it. Any help appreciated.

推荐答案

没有特定功能,但您可以使用以下方法实现相同功能:

There is no specific function for it, but you can achieve the same with:

# load package
library(data.table)

# convert to a 'data.table'
setDT(df)

# expand and fill the dataset with a rolling join
df[.(c(0, depth)), on = .(depth), roll = -Inf]

给出:


                    filename depth        ps
1: PS92_019-6_rovT_irrad.tab     0 3.2622340
2: PS92_019-6_rovT_irrad.tab     5 3.2622340
3: PS92_019-6_rovT_irrad.tab    10 3.3894795
4: PS92_019-6_rovT_irrad.tab    20 3.9738059
5: PS92_019-6_rovT_irrad.tab    75 0.4280748


直接向@Frank提出改进建议。

Heads up to @Frank for the improvement suggestion.

旧解:

df[CJ(depth = c(0,unique(depth))), on = 'depth'
   ][, c(1,3) := lapply(.SD, zoo::na.locf, fromLast = TRUE), .SDcols = c(1,3)][]

这篇关于来自tidyr的完整/填充的data.table等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆