tidyr::complete()将行添加到a data.frame中,以获取数据中缺少的列值组合.例:
library(dplyr)
library(tidyr)
df <- data.frame(person = c(1,2,2),
observation_id = c(1,1,2),
value = c(1,1,1))
df %>%
tidyr::complete(person,
observation_id,
fill = list(value=0))
Run Code Online (Sandbox Code Playgroud)
产量
# A tibble: 4 × 3
person observation_id value
<dbl> <dbl> <dbl>
1 1 1 1
2 1 2 0
3 2 1 1
4 2 2 1
Run Code Online (Sandbox Code Playgroud)
其中value组合person == 1和observation_id == 2缺少的组合df已填入值0.
什么相当于这个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", …Run Code Online (Sandbox Code Playgroud)