R为什么完整功能仅适用于部分数据帧?

时间:2019-06-26 21:07:19

标签: r dplyr complete

我正在尝试完成一个数据帧中的隐式缺失值,该数据帧描述了两个站点(WAI和HAN)的所有12个模块的所有3个方面(N,S和T)的3个不同藻类类别的覆盖率。某些封面数据丢失是因为“标签”(T,MA,CCA)特别是在“ WAI”站点的“标签”列中填充“ CCA”的隐式缺失值时遇到的问题。

我相信我遇到此问题的原因是,WAI网站上的大多数侧面和模块都缺少“ CCA”。但是,我不确定该如何解决。

此处的最终目标是使每个日期,站点,模块和侧面组合具有所有三个类别(T,MA,CCA)。如果缺少这三个类别中的任何一个,我希望n = 0并且percent_cover =0。这样,所有隐式丢失的值都将被显式显示。

如前所述,我在dplyr中使用了完整函数来填充隐式缺失的“标签”类别(T,MA,CCA)。但是,所有“日期”,“站点”,“模块”和“侧面”组合都不包括所有三个标签,尤其是对于WAI站点。

MA_cover_final <- structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 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 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L), .Label = c("HAN", "WAI"), class = "factor"), Module = c(7L, 
    7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 
    9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
    6L, 6L, 6L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 
    9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 
    11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
    4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
    6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 
    9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 
    11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
    4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 
    6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 
    9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 
    11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L
    ), Side = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 1L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1L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 
    1L, 2L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 
    2L, 3L), .Label = c("N", "S", "T"), class = "factor"), nn = c(50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 49L, 49L, 49L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 49L, 
    49L, 49L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 46L, 46L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 49L, 49L, 51L, 51L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 47L, 47L, 50L, 51L, 51L, 50L, 50L, 50L, 
    50L, 41L, 41L, 48L, 48L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 49L, 
    49L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 49L, 50L, 50L, 50L, 50L, 50L, 50L, 49L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    51L, 51L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 49L, 49L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 49L, 49L, 49L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L), Label = c("CCA", "MA", "T", "CCA", "MA", 
    "T", "CCA", "T", "MA", "T", "MA", "T", "MA", "T", "CCA", 
    "MA", "T", "CCA", "MA", "T", "MA", "T", "MA", "T", "CCA", 
    "MA", "T", "CCA", "T", "CCA", "MA", "T", "CCA", "T", "T", 
    "CCA", "MA", "T", "MA", "T", "T", "MA", "T", "CCA", "MA", 
    "T", "T", "MA", "T", "T", "MA", "T", "MA", "T", "T", "T", 
    "MA", "T", "MA", "T", "MA", "T", "MA", "T", "MA", "T", "MA", 
    "T", "MA", "T", "MA", "T", "MA", "T", "T", "T", "MA", "T", 
    "CCA", "MA", "T", "T", "CCA", "MA", "T", "MA", "T", "CCA", 
    "T", "T", "MA", "T", "MA", "T", "CCA", "MA", "T", "CCA", 
    "MA", "T", "T", "CCA", "T", "MA", "T", "T", "MA", "T", "MA", 
    "T", "T", "MA", "T", "MA", "T", "T", "MA", "T", "MA", "T", 
    "MA", "T", "MA", "T", "MA", "T", "MA", "T", "MA", "T", "MA", 
    "T", "MA", "T", "MA", "T", "MA", "T", "MA", "T", "MA", "T", 
    "MA", "T", "MA", "T", "MA", "T", "MA", "T", "MA", "T", "MA", 
    "T", "MA", "T", "T", "MA", "T", "MA", "T", "MA", "T", "MA", 
    "T", "MA", "T", "T", "MA", "T", "MA", "T", "MA", "T", "MA", 
    "T", "T", "T", "MA", "T", "T", "T", "CCA", "MA", "T", "CCA", 
    "MA", "T", "CCA", "T", "MA", "T", "MA", "T", "T", "CCA", 
    "MA", "T", "CCA", "MA", "T", "T", "CCA", "MA", "T", "CCA", 
    "MA", "T", "T", "T", "MA", "T", "T", "MA", "T", "MA", "T", 
    "MA", "T", "MA", "T", "MA", "T", "MA", "T", "MA", "T", "MA", 
    "T", "MA", "T", "T", "MA", "T", "MA", "T", "MA", "T", "MA", 
    "T", "MA", "T", "CCA", "MA", "T", "CCA", "MA", "T", "CCA", 
    "T", "MA", "T", "MA", "T", "MA", "T", "CCA", "MA", "T", "CCA", 
    "MA", "T", "T", "MA", "T", "MA", "T", "T", "CCA", "MA", "T", 
    "CCA", "MA", "T", "MA", "T", "MA", "T", "MA", "T", "T"), 
    n = c(1L, 5L, 34L, 3L, 2L, 39L, 1L, 6L, 5L, 37L, 4L, 38L, 
    3L, 9L, 1L, 3L, 26L, 2L, 6L, 28L, 1L, 9L, 3L, 29L, 1L, 6L, 
    34L, 1L, 7L, 3L, 1L, 28L, 1L, 16L, 5L, 1L, 6L, 39L, 5L, 37L, 
    4L, 1L, 48L, 1L, 2L, 42L, 39L, 3L, 43L, 45L, 1L, 37L, 3L, 
    39L, 38L, 47L, 5L, 34L, 2L, 40L, 6L, 40L, 6L, 42L, 3L, 46L, 
    1L, 45L, 4L, 40L, 3L, 42L, 3L, 39L, 46L, 48L, 3L, 31L, 1L, 
    1L, 36L, 10L, 2L, 1L, 43L, 1L, 42L, 1L, 1L, 36L, 1L, 33L, 
    1L, 9L, 1L, 1L, 45L, 3L, 5L, 36L, 6L, 1L, 41L, 1L, 40L, 7L, 
    3L, 43L, 3L, 41L, 34L, 4L, 45L, 2L, 44L, 29L, 8L, 39L, 6L, 
    40L, 2L, 34L, 8L, 31L, 2L, 40L, 1L, 35L, 3L, 46L, 5L, 42L, 
    1L, 41L, 2L, 43L, 3L, 44L, 1L, 35L, 3L, 44L, 7L, 43L, 1L, 
    48L, 7L, 38L, 2L, 40L, 3L, 40L, 6L, 37L, 9L, 38L, 44L, 1L, 
    39L, 4L, 27L, 4L, 44L, 5L, 42L, 9L, 38L, 48L, 5L, 36L, 8L, 
    33L, 3L, 38L, 1L, 47L, 50L, 12L, 2L, 31L, 33L, 30L, 1L, 3L, 
    40L, 3L, 1L, 38L, 1L, 21L, 3L, 32L, 1L, 29L, 29L, 2L, 3L, 
    38L, 2L, 3L, 36L, 15L, 1L, 1L, 35L, 1L, 3L, 35L, 24L, 44L, 
    2L, 46L, 42L, 4L, 42L, 3L, 44L, 3L, 24L, 2L, 45L, 3L, 40L, 
    3L, 46L, 2L, 42L, 6L, 42L, 1L, 41L, 46L, 5L, 41L, 1L, 42L, 
    5L, 41L, 4L, 36L, 3L, 31L, 2L, 5L, 34L, 4L, 4L, 23L, 1L, 
    3L, 13L, 28L, 7L, 40L, 4L, 28L, 1L, 1L, 43L, 3L, 2L, 41L, 
    16L, 4L, 34L, 3L, 31L, 5L, 1L, 4L, 25L, 4L, 4L, 28L, 1L, 
    3L, 2L, 46L, 3L, 41L, 4L), percent_cover = c(0.02, 0.1, 0.68, 
    0.06, 0.04, 0.78, 0.02, 0.12, 0.1, 0.74, 0.08, 0.76, 0.06, 
    0.18, 0.0204081632653061, 0.0612244897959184, 0.530612244897959, 
    0.04, 0.12, 0.56, 0.02, 0.18, 0.06, 0.58, 0.0204081632653061, 
    0.122448979591837, 0.693877551020408, 0.02, 0.14, 0.06, 0.02, 
    0.56, 0.02, 0.32, 0.1, 0.02, 0.12, 0.78, 0.1, 0.74, 0.08, 
    0.02, 0.96, 0.02, 0.04, 0.84, 0.78, 0.06, 0.86, 0.9, 0.0217391304347826, 
    0.804347826086957, 0.06, 0.78, 0.76, 0.94, 0.1, 0.68, 0.04, 
    0.8, 0.12, 0.8, 0.12, 0.84, 0.06, 0.92, 0.02, 0.9, 0.0816326530612245, 
    0.816326530612245, 0.0588235294117647, 0.823529411764706, 
    0.06, 0.78, 0.92, 0.96, 0.06, 0.62, 0.02, 0.02, 0.72, 0.2, 
    0.04, 0.02, 0.86, 0.02, 0.84, 0.02, 0.02, 0.72, 0.02, 0.66, 
    0.02, 0.18, 0.02, 0.02, 0.9, 0.06, 0.1, 0.72, 0.12, 0.02, 
    0.82, 0.02, 0.8, 0.14, 0.06, 0.86, 0.06, 0.82, 0.68, 0.08, 
    0.9, 0.0425531914893617, 0.936170212765957, 0.58, 0.156862745098039, 
    0.764705882352941, 0.12, 0.8, 0.04, 0.68, 0.195121951219512, 
    0.75609756097561, 0.0416666666666667, 0.833333333333333, 
    0.02, 0.7, 0.06, 0.92, 0.1, 0.84, 0.02, 0.82, 0.04, 0.86, 
    0.06, 0.88, 0.02, 0.7, 0.06, 0.88, 0.14, 0.86, 0.0204081632653061, 
    0.979591836734694, 0.14, 0.76, 0.04, 0.8, 0.06, 0.8, 0.12, 
    0.74, 0.18, 0.76, 0.88, 0.02, 0.78, 0.08, 0.54, 0.08, 0.88, 
    0.1, 0.84, 0.18, 0.76, 0.96, 0.1, 0.72, 0.16, 0.66, 0.06, 
    0.76, 0.02, 0.94, 1, 0.24, 0.04, 0.62, 0.66, 0.6, 0.02, 0.06, 
    0.8, 0.06, 0.02, 0.76, 0.02, 0.42, 0.06, 0.64, 0.02, 0.58, 
    0.591836734693878, 0.04, 0.06, 0.76, 0.04, 0.06, 0.72, 0.306122448979592, 
    0.02, 0.02, 0.7, 0.02, 0.06, 0.7, 0.48, 0.88, 0.04, 0.92, 
    0.84, 0.08, 0.84, 0.06, 0.88, 0.0588235294117647, 0.470588235294118, 
    0.04, 0.9, 0.06, 0.8, 0.06, 0.92, 0.04, 0.84, 0.12, 0.84, 
    0.02, 0.82, 0.92, 0.1, 0.82, 0.02, 0.84, 0.102040816326531, 
    0.836734693877551, 0.08, 0.72, 0.06, 0.62, 0.04, 0.1, 0.68, 
    0.08, 0.08, 0.46, 0.02, 0.06, 0.26, 0.56, 0.14, 0.8, 0.08, 
    0.56, 0.0204081632653061, 0.0204081632653061, 0.877551020408163, 
    0.06, 0.04, 0.82, 0.32, 0.08, 0.68, 0.06, 0.62, 0.1, 0.02, 
    0.08, 0.5, 0.08, 0.08, 0.56, 0.02, 0.06, 0.04, 0.92, 0.06, 
    0.82, 0.08)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-281L))

MA_cover_final <- MA_cover_final %>% group_by(Date, Site) %>% 
  complete(Side, Label, fill = list(n = 0, percent_cover = 0)) %>% 
  ungroup()

结果数据框应具有432行[12个模块(1-12)x 3面(N,S,T)x 3标签(“ T”,“ MA”,“ CCA”)x 4日期]

2 个答案:

答案 0 :(得分:1)

我想我已经确定了您要寻找的东西,但是您必须确认:

我们要完成每个因子列,但Date例外,它在Site之间是离散的,因此,我们将其包装在nesting()中以仅使用Site和数据中存在的日期。

final <- MA_cover_final %>% group_by(Site) %>% 
  complete(Label, Side, Module, nesting(Date), fill = list(n= 0, percent_cover =0))


# A tibble: 432 x 8
# Groups:   Site [2]
   Site  Label Side  Module Date       nn     n percent_cover
   <fct> <chr> <fct>  <int> <fct>   <int> <dbl>         <dbl>
 1 HAN   CCA   N          7 4/11/17    50     1        0.02  
 2 HAN   CCA   N          7 6/12/18    NA     0        0     
 3 HAN   CCA   N          7 8/28/18    NA     0        0     
 4 HAN   CCA   N          7 9/5/17     50     2        0.04  
 5 HAN   CCA   N          8 4/11/17    NA     0        0     
 6 HAN   CCA   N          8 6/12/18    NA     0        0     
 7 HAN   CCA   N          8 8/28/18    NA     0        0     
 8 HAN   CCA   N          8 9/5/17     NA     0        0     
 9 HAN   CCA   N          9 4/11/17    49     1        0.0204
10 HAN   CCA   N          9 6/12/18    50     2        0.04  
# ... with 422 more rows

答案 1 :(得分:0)

如果要在分组数据集上使用complete(),则不会扩展该组中不存在的因子水平。相反,我们必须提供要添加到每个组的级别。这会在您的数据集中出现Label

此外,我认为您需要Module成为您采用的任何方法的一部分。

似乎模块嵌套在站点中,而站点嵌套在日期中,所以我认为您不希望为整个数据集填充这些模块的组合。您可以将它们用作分组变量,然后在Side中使用另外两个变量Labelcomplete()。由于这些值仅在组内使用,因此我们需要为Label定义值。 (对于您的示例,Side可能还可以,但也应谨慎提供Side的值。)

test <- MA_cover_final %>% 
    group_by(Site, Date, Module) %>% 
    complete(Side, Label = unique(test$Label), fill = list(n = 0, percent_cover = 0)) %>% 
    ungroup()

str(test)

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   432 obs. of  8 variables:
 $ Date         : Factor w/ 8 levels "4/11/17","4/23/17",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Site         : Factor w/ 2 levels "HAN","WAI": 1 1 1 1 1 1 1 1 1 1 ...
 $ Module       : int  7 7 7 7 7 7 7 7 7 8 ...
 $ Side         : Factor w/ 3 levels "N","S","T": 1 1 1 2 2 2 3 3 3 1 ...
 $ Label        : chr  "CCA" "MA" "T" "CCA" ...
 $ nn           : int  50 50 50 50 50 50 50 NA 50 NA ...
 $ n            : num  1 5 34 3 2 39 1 0 6 0 ...
 $ percent_cover: num  0.02 0.1 0.68 0.06 0.04 0.78 0.02 0 0.12 0 ...

如果获得正确的嵌套和交叉组合,则无需分组即可完成所有操作。您可能会追求以下类似的东西。这仅保留嵌套变量的组内组合,但填充缺失的SideLabel值:

    test2 <- MA_cover_final %>% 
        complete(nesting(Site, Date, Module), Side, Label, fill = list(n = 0, percent_cover = 0))

str(test)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   432 obs. of  8 variables:
 $ Site         : Factor w/ 2 levels "HAN","WAI": 1 1 1 1 1 1 1 1 1 1 ...
 $ Date         : Factor w/ 8 levels "4/11/17","4/23/17",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Module       : int  7 7 7 7 7 7 7 7 7 8 ...
 $ Side         : Factor w/ 3 levels "N","S","T": 1 1 1 2 2 2 3 3 3 1 ...
 $ Label        : chr  "CCA" "MA" "T" "CCA" ...
 $ nn           : int  50 50 50 50 50 50 50 NA 50 NA ...
 $ n            : num  1 5 34 3 2 39 1 0 6 0 ...
 $ percent_cover: num  0.02 0.1 0.68 0.06 0.04 0.78 0.02 0 0.12 0 ...

identical(test, test2)
[1] TRUE