【问题标题】:matching sites in different dataframes in RR中不同数据框中的匹配站点
【发布时间】:2017-10-05 16:42:31
【问题描述】:

我有多个如下所示的数据框: 列中有许多物种,我不在这里报告。 d1:

Year   Region  Sites Depth Transect Pharia pyramidatus
2000   LP     BALLENA      5        1        0.03
2000   LP     ISLOTES      5        1        0.20
2000   LP     NORTE        5        1        0.10
2000   LP     NORTE       20        1        0.00

d2

Year   Region  Sites      Depth Transect Pharia pyramidatus
2010   LP     PLAYA        5        1        0.03
2010   LP     ISLOTES      5        1        0.20
2010   LP     NORTE        5        1        0.10
2010   LP     NORTE       20        1        0.00

d3

Year   Region  Sites      Depth Transect Pharia pyramidatus
2016   LP     BALLENA      5        1        0.03
2016   LP     ISLOTES      5        1        0.20
2016   LP     SUR          5        1        0.10
2016   LP     NORTE       20        1        0.00

我想做的是提取仅在 每一 年出现的相同站点 (Reef),并将结果 rbind 到一个应如下所示的数据框中:

Year   Region  Reef      Depth Transect Pharia pyramidatus
2000   LP     ISLOTES      5        1        0.20
2000   LP     NORTE        5        1        0.10
2000   LP     NORTE       20        1        0.00
2010   LP     ISLOTES      5        1        0.20
2010   LP     NORTE        5        1        0.10
2010   LP     NORTE       20        1        0.00
2016   LP     ISLOTES      5        1        0.20
2016   LP     NORTE        20       1        0.00

非常感谢您的帮助

【问题讨论】:

  • “网站”是指Reef?
  • 另外,NORTE 不存在于d3 中,那么为什么要将它包含在最终的df 中?
  • 查看更新数据的解决方案

标签: r dataframe subset extract


【解决方案1】:

dplyr 的解决方案:

library(dplyr)
rbind(df1, df2, df3) %>%
  group_by(Reef) %>%
  filter(n_distinct(Year) == 3)

结果:

# A tibble: 8 x 6
# Groups:   Reef [2]
   Year Region    Reef Depth Transect Pharia_pyramidatus
  <int> <fctr>  <fctr> <int>    <int>              <dbl>
1  2000     LP ISLOTES     5        1                0.2
2  2000     LP   NORTE     5        1                0.1
3  2000     LP   NORTE    20        1                0.0
4  2010     LP ISLOTES     5        1                0.2
5  2010     LP   NORTE     5        1                0.1
6  2010     LP   NORTE    20        1                0.0
7  2016     LP ISLOTES     5        1                0.2
8  2016     LP   NORTE    20        1                0.0

注意事项:

n_distinct 计算每个Reef 的不同Year 的数量(因为我是group_by(Reef))。我想要distinct_n == 3,因为我只想返回Reef 对每个Year 都有记录的行,在这种情况下为3 年。在更一般的情况下,如果有更多 Year,您可能希望首先找到数据框的 Year 跨度,然后基于此找到 filter,如下所示:

rbind(df1, df2, df3) %>%
  mutate(Year_distinct = n_distinct(Year)) %>%
  group_by(Reef) %>%
  filter(n_distinct(Year) == Year_distinct) %>%
  select(-Year_distinct)

数据:

df1 = read.table(text = "Year   Region  Reef      Depth Transect Pharia_pyramidatus
                 2000   LP     BALLENA      5        1        0.03
                 2000   LP     ISLOTES      5        1        0.20
                 2000   LP     NORTE        5        1        0.10
                 2000   LP     NORTE       20        1        0.00", header = TRUE)

df2 = read.table(text = "Year   Region  Reef      Depth Transect Pharia_pyramidatus
                 2010   LP     PLAYA        5        1        0.03
                 2010   LP     ISLOTES      5        1        0.20
                 2010   LP     NORTE        5        1        0.10
                 2010   LP     NORTE       20        1        0.00", header = TRUE)

df3 = read.table(text = "Year   Region  Reef      Depth Transect Pharia_pyramidatus
                 2016   LP     BALLENA      5        1        0.03
                 2016   LP     ISLOTES      5        1        0.20
                 2016   LP     SUR          5        1        0.10
                 2016   LP     NORTE         20        1        0.00", header = TRUE)

【讨论】:

  • 感谢您的回复,它解决了问题,我有一个问题,n_distinct(Year) == 3 是如何工作的?是 3 因为三个数据框吗?
  • @FabioFavoretto 在我的回答中添加了解释。我还添加了一个更通用的版本,以防您不想在数据框中硬编码 Year 的数量。
  • 你太棒了!感谢您的快速而有帮助的回复!
  • 我注意到在一般情况下,在输出中添加了一个带有“Year_distinct”的附加列,这不是问题,只是如果有人想使用此功能并且不这样做,它应该是一个警告发现添加的新列;)
  • @FabioFavoretto 如果您不想在最终数据集中出现Year_distinct 列,您可以添加%&gt;% select(-Year_distinct) :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多