合并具有相同日期和站点的行

huangapple go评论97阅读模式
英文:

Combine rows with same date and site

问题

我有一个像这样的数据框:

合并具有相同日期和站点的行

我想将具有相同站点ID和日期的物质的值移动到一行。我该怎么做?也许可以用aggregatematchmerge吗?

英文:

I have a dataframe like this:

合并具有相同日期和站点的行

I want to move the values of the substances that have the same station ID and date on an unique row. How can I do that? maybe with aggregateor matchor merge?

答案1

得分: 0

如果我们想要删除NA元素,只保留非NA元素,可以进行分组汇总并得到sum

  1. library(dplyr)
  2. df1 %>%
  3. group_by(station, date) %>%
  4. summarise(across(everything(),
  5. ~ sum(.x, na.rm = TRUE)), .groups = 'drop')

或者在分组后使用条件返回第一个非NA值。

  1. df1 %>%
  2. group_by(station, date) %>%
  3. summarise(across(everything(), ~if(all(is.na(.x))) NA else
  4. .x[!is.na(.x)][1]))

或者使用base R方法:

  1. aggregate(.~ station + date, df1, sum, na.rm = TRUE, na.action = NULL)
英文:

If we want to remove the NA elements and keep only the non-NA, do a grouping by summarise and get the sum

  1. library(dplyr)
  2. df1 %>%
  3. group_by(station, date) %>%
  4. summarise(across(everything(),
  5. ~ sum(.x, na.rm = TRUE)), .groups = 'drop')

Or use a condition to return the first non-NA after grouping

  1. df1 %>%
  2. group_by(station, date) %>%
  3. summarise(across(everything(), ~if(all(is.na(.x))) NA else
  4. .x[!is.na(.x)][1]))

Or with base R

  1. aggregate(.~ station + date, df1, sum, na.rm = TRUE, na.action = NULL)

答案2

得分: -1

你是指这个吗?

  1. library(tidyverse)
  2. # 定义数据集
  3. df1 <- tibble::tribble(
  4. ~station, ~date, ~substance1,
  5. 1, "08/07/2009", "5.7",
  6. 2, "13/07/2009", "6",
  7. 3, "13/07/2009", "2.2",
  8. 4, "13/07/2009", "2",
  9. 5, "22/09/2009", "1"
  10. )
  11. df2 <- tibble::tribble(
  12. ~station, ~date, ~substance4,
  13. 1, "08/07/2009", 2,
  14. 2, "13/07/2009", 6,
  15. 4, "13/07/2009", 3
  16. )
  17. # 按照站点和日期合并
  18. full_join(df1, df2)
  19. #> Joining with `by = join_by(station, date)`
  20. #> # A tibble: 5 × 4
  21. #> station date substance1 substance4
  22. #> <dbl> <chr> <chr> <dbl>
  23. #> 1 1 08/07/2009 5.7 2
  24. #> 2 2 13/07/2009 6 6
  25. #> 3 3 13/07/2009 2.2 NA
  26. #> 4 4 13/07/2009 2 3
  27. #> 5 5 22/09/2009 1 NA

创建于2023-03-03,使用reprex v2.0.2

英文:

Do you mean this?

  1. library(tidyverse)
  2. # define datasets
  3. df1 &lt;- tibble::tribble(
  4. ~station, ~date, ~substance1,
  5. 1, &quot;08/07/2009&quot;, &quot;5.7&quot;,
  6. 2, &quot;13/07/2009&quot;, &quot;6&quot;,
  7. 3, &quot;13/07/2009&quot;, &quot;2.2&quot;,
  8. 4, &quot;13/07/2009&quot;, &quot;2&quot;,
  9. 5, &quot;22/09/2009&quot;, &quot;1&quot;
  10. )
  11. df2 &lt;- tibble::tribble(
  12. ~station, ~date, ~substance4,
  13. 1, &quot;08/07/2009&quot;, 2,
  14. 2, &quot;13/07/2009&quot;, 6,
  15. 4, &quot;13/07/2009&quot;, 3
  16. )
  17. # merge by station and date
  18. full_join(df1, df2)
  19. #&gt; Joining with `by = join_by(station, date)`
  20. #&gt; # A tibble: 5 &#215; 4
  21. #&gt; station date substance1 substance4
  22. #&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;
  23. #&gt; 1 1 08/07/2009 5.7 2
  24. #&gt; 2 2 13/07/2009 6 6
  25. #&gt; 3 3 13/07/2009 2.2 NA
  26. #&gt; 4 4 13/07/2009 2 3
  27. #&gt; 5 5 22/09/2009 1 NA

<sup>Created on 2023-03-03 with reprex v2.0.2</sup>

Please be sure to include code to create the datasets and a proper description of your desired output next time.

huangapple
  • 本文由 发表于 2023年3月4日 01:57:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定