【发布时间】:2021-12-23 18:50:23
【问题描述】:
我是 R 环境的新手,我想提高我的“操作数据”技能。我有这个数据集:
structure(list(taxon = c("Acroloxus lacustris", "Ancylus fluviatilis",
"Ancylus fluviatilis", "Asellus aquaticus", "Asellus aquaticus",
"Asellus aquaticus", "Bithynia tentaculata", "Bryozoa Gen. sp.",
"Chironomidae Gen. sp.", "Ephydatia fluviatilis", "Erpobdella octoculata",
"Erpobdella octoculata", "Erpobdella octoculata", "Glossiphonia complanata",
"Physella acuta", "Plumatella fungosa", "Plumatella fungosa",
"Plumatella repens", "Radix balthica/labiata", "Radix balthica/labiata",
"Radix balthica/labiata", "Sphaerium corneum", "Spongilla lacustris",
"Spongillidae Gen. sp.", "Tubificidae Gen. sp."), year = c(1971,
1969, 1971, 1968, 1969, 1971, 1971, 1971, 1969, 1971, 1968, 1969,
1971, 1971, 1971, 1968, 1971, 1971, 1968, 1969, 1971, 1971, 1969,
1971, 1971), abundance = c(12.5714285714286, 6, 15.5, 1, 13,
100.333333333333, 2.11111111111111, 13, 6, 7, 20, 42.5, 22.875,
1, 1, 20, 3.5, 2.66666666666667, 20, 42.5, 17.5789473684211,
65, 6, 42.5, 1)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -25L), groups = structure(list(taxon = c("Acroloxus lacustris",
"Ancylus fluviatilis", "Asellus aquaticus", "Bithynia tentaculata",
"Bryozoa Gen. sp.", "Chironomidae Gen. sp.", "Ephydatia fluviatilis",
"Erpobdella octoculata", "Glossiphonia complanata", "Physella acuta",
"Plumatella fungosa", "Plumatella repens", "Radix balthica/labiata",
"Sphaerium corneum", "Spongilla lacustris", "Spongillidae Gen. sp.",
"Tubificidae Gen. sp."), .rows = structure(list(1L, 2:3, 4:6,
7L, 8L, 9L, 10L, 11:13, 14L, 15L, 16:17, 18L, 19:21, 22L,
23L, 24L, 25L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -17L), .drop = TRUE))
我想将此数据集重新排序为这种格式:
列:物种 1、物种 2、物种 3 .... 行:年(1968、1969 和 1971,即 3 行) 如您所见,并非所有物种都出现在所有年份 看起来像下一个:
Sp1 Sp2...
1968 1 0
1969 0 45
1971 10 0
但我不知道如何重新排序。我认为%>% t() 或mutate() 可能有用...
感谢您的宝贵时间。
【问题讨论】:
-
我加载了这个,将其分配给一个变量,并将其转换为数据帧而没有错误。您应该查看
tidyr包,特别是spread、gather、separate、unite函数。这是一个很好的介绍:rstudio-education.github.io/tidyverse-cookbook/tidy.html -
@LSM-DAT_Linux 但是请注意,
pivot_wider和pivot_longer现在是spread和gather的首选选项(已被取代)。 -
@Andrew Gillreath-Brown 如果我给它们起别名,则不会;)我更喜欢传播,聚集为动词。 pivot_wider、pivot_longer 听起来像是由 Excel 阴谋集团命名的。