r - 如何组合具有相同行名但唯一值的小标题行?
问题描述
我有以下小标题:
Symbol `Annotated Term`
<chr> <chr>
1 H2aj chromatin silencing
2 Sfpq histone H3 deacetylation
3 Ube2n histone ubiquitination
4 Ube2n positive regulation of histone modification
如何将重复的符号组合成一行,并将它们的注释术语组合在同一行中,以便上面的小标题看起来像:
Symbol `Annotated Term`
<chr> <chr>
1 H2aj chromatin silencing
2 Sfpq histone H3 deacetylation
3 Ube2n histone ubiquitination, positive regulation of histone modification
任何帮助将不胜感激
解决方案
那要看。如果您希望将字符串连接并统一为单个字符串,那么
opt1 <- aggregate(`Annotated Term` ~ Symbol, data = dat, FUN = toString)
opt1
# Symbol Annotated Term
# 1 H2aj chromatin silencing
# 2 Sfpq histone H3 deacetylation
# 3 Ube2n histone ubiquitination, positive regulation of histone modification
str(opt1)
# 'data.frame': 3 obs. of 2 variables:
# $ Symbol : chr "H2aj" "Sfpq" "Ube2n"
# $ Annotated Term: chr "chromatin silencing" "histone H3 deacetylation" "histone ubiquitination, positive regulation of histone modification"
如果您希望稍后轻松拆分它们,那么您的`Annotated Term`
列需要保留为列表列,在这种情况下:
opt2 <- aggregate(`Annotated Term` ~ Symbol, data = dat, FUN = list)
opt2
# Symbol Annotated Term
# 1 H2aj chromatin silencing
# 2 Sfpq histone H3 deacetylation
# 3 Ube2n histone ubiquitination, positive regulation of histone modification
str(opt2)
# 'data.frame': 3 obs. of 2 variables:
# $ Symbol : chr "H2aj" "Sfpq" "Ube2n"
# $ Annotated Term:List of 3
# ..$ : chr "chromatin silencing"
# ..$ : chr "histone H3 deacetylation"
# ..$ : chr "histone ubiquitination" "positive regulation of histone modification"
(如果您还不了解列表列,并且相信您以后不会对取消组合它们感兴趣,那么我建议您opt1
。)
数据
dat <- structure(list(Symbol = c("H2aj", "Sfpq", "Ube2n", "Ube2n"), `Annotated Term` = c("chromatin silencing", "histone H3 deacetylation", "histone ubiquitination", "positive regulation of histone modification")), row.names = c(NA, -4L), class = "data.frame")
推荐阅读
- python - 尝试使用 selenium 对 ncbi 进行 webscrape,数据未加载且未包含在具有我可以等待的 ID 的元素中
- java - JUnit5 - 从 json 文件动态展示测试用例名称
- wordpress - Cloudflare 和 Google Cloud Platform 出现错误 522
- asp.net - 如何设置 asp.net roleManager cookie samesite 属性?
- python - 以正确的顺序选择列表中价值最高的项目
- javascript - 当我在图像下有文字时,如何使箭头相对于光滑滑块中的图片垂直居中?
- python - 如何在 django 中使用相应模型对象的属性的 gte 进行过滤
- powershell - Powershell 将空内容而不是空内容添加到一个 CSV
- c# - Blazor C# 日期显示为 1/1/0001 12:00:00Am
- rust - 为什么当具体类型起作用时,引用同级字段的迭代器特征对象无法编译?