r - How can I merge row with similar value in column, to get unique value in another column in R
问题描述
Hei,
I would like to merge rows under conditions, when the column NameSize has the same value (example: Chaetoceros), I would like to get the combined value of the columns: August 2018 and August 2019 into respective column by keeping unique value only.
I have this case for many others species in the column NameSize (several more Chaetoceros and others).
Example of part of my data set
Results expected
Example of part of my data with dput():
structure(list(class = c("Bacillariophyceae", "Bacillariophyceae",
"Bacillariophyceae", "Bacillariophyceae", "Bacillariophyceae",
"Bacillariophyceae"), NameSize = c("Attheya longicornis", "Bacterosira bathyomphala",
"Chaetoceros", "Chaetoceros", "Chaetoceros cf. atlanticus", "Chaetoceros cf. borealis"
), `August 2018` = c("SICE3", "P1,SICE3", "P1,PICE1,SICE3", "PICE1",
"SICE3", "P1,PICE1,SICE3"), `August 2019` = c("P6,P7,Sice4",
"P6", "", "P2", "", "")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
results wanted:
structure(list(class = c("Bacillariophyceae", "Bacillariophyceae",
"Bacillariophyceae", "Bacillariophyceae", "Bacillariophyceae"
), NameSize = c("Attheya longicornis", "Bacterosira bathyomphala",
"Chaetoceros", "Chaetoceros cf. atlanticus", "Chaetoceros cf. borealis"
), `Aug-18` = c("SICE3", "P1,SICE3", "P1,PICE1,SICE3", "SICE3",
"P1,PICE1,SICE3"), `Aug-19` = c("P6,P7,Sice4", "P6", "P2", NA,
NA)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
-5L), spec = structure(list(cols = list(class = structure(list(), class = c("collector_character",
"collector")), NameSize = structure(list(), class = c("collector_character",
"collector")), `Aug-18` = structure(list(), class = c("collector_character",
"collector")), `Aug-19` = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
解决方案
p <- structure(list(class = c("Bacillariophyceae", "Bacillariophyceae",
"Bacillariophyceae", "Bacillariophyceae", "Bacillariophyceae",
"Bacillariophyceae"), NameSize = c("Attheya longicornis", "Bacterosira bathyomphala",
"Chaetoceros", "Chaetoceros", "Chaetoceros cf. atlanticus", "Chaetoceros cf. borealis"
), `August 2018` = c("SICE3", "P1,SICE3", "P1,PICE1,SICE3", "PICE1",
"SICE3", "P1,PICE1,SICE3"), `August 2019` = c("P6,P7,Sice4",
我使用图书馆“pdlyr”做到了这一点
p1 <- p %>% group_by(class, NameSize) %>%
summarise(`August 2018` = first(`August 2018`),
`August 2019` = last(`August 2019`)) %>%
as.data.frame()
希望这对你有用
class NameSize `August 2018` `August 2019`
<chr> <chr> <chr> <chr>
1 Bacillariophyceae Attheya longicornis SICE3 "P6,P7,Sice4"
2 Bacillariophyceae Bacterosira bathyomphala P1,SICE3 "P6"
3 Bacillariophyceae Chaetoceros P1,PICE1,SICE3 "P2"
4 Bacillariophyceae Chaetoceros cf. atlanticus SICE3 ""
5 Bacillariophyceae Chaetoceros cf. borealis P1,PICE1,SICE3 ""
推荐阅读
- oracle - 从 Netsuite 摘要保存的搜索中删除“总计”行
- java - 为什么这个多线程计数器会产生正确的结果?
- sql-server - SQL Server 2012 的 OPenQuery(SET FTMONLY OFF)问题
- xml - 如何在 xsd 中声明包含一系列嵌套子元素的照片元素?
- reactjs - 使用自定义 Route 组件时 React Router 匹配属性 null
- c++ - 在 Linux 上使用 SDL2 确定最大窗口大小
- python - Python 包在第一次尝试时未安装,但在第二次尝试时安装
- javascript - Node JS 异步/等待控制器问题
- mongodb - MongoDB - 查询集合以返回所有最新版本的文档
- javascript - RESTful API 中的 Typescript 自定义错误