【发布时间】:2020-11-11 13:33:57
【问题描述】:
我有以下格式的数据:
dataset <- data.frame(taxa = c("k__Archaea| p__Crenarchaeota", "k__Archaea| p__Euryarchaeota", "k__Bacteria| p__[Thermi]"),
"11908.MM.0008.Inf.6m.Stool" =c(0,1760,0),
"11908.MM.01115.Inf.6m.Stool" =c(0,1517,0),
"11908.MM.0044.Inf.6m.Stool" =c(0,10815,0),
"11908.MM.0125.Mom.6m.Stool" = c(0,4719,0))
view(dataset)
我想将其转换为以下格式:
fix_dataset <- data.frame(study_id = c(0008, 0115, 0044, 0125),
individual = c("Inf", "Inf", "Inf", "Mom" ),
`k__Archaea| p__Crenarchaeota` = c(0,0,0,0),
`k__Archaea| p__Euryarchaeota`= c(1760, 1517,10815, 4719),
`` = c(0,0,0,0),
timept1 = c("6m", "6m", "6m", "6m"))
view(fix_dataset)
我正在尝试从每个列名中删除开头的数字系列 11908 和“Stool”,拆分列名的其他部分并从宽格式转换为长格式。
我正在使用以下代码
library(tidyverse)
dataset %>%
pivot_longer(cols = -taxa) %>%
separate(col = name, into = c("info1", "info2", "study_id", "individual", "timept1", "info3"), sep = "[.]") %>%
pivot_wider(names_from = taxa,
values_from = value) %>%
select(study_id, individual, starts_with("k_"), timept1)
当我将其应用于我的数据时,我收到以下错误消息
Error in select(., study_id, individual, timept1, starts_with("k_")) :
unused arguments (study_id, individual, timept1, starts_with("k_"))
In addition: Warning messages:
1: Expected 6 pieces. Additional pieces discarded in 44 rows [242, 243, 903, 904, 1564, 1565, 2225, 2226, 2886, 2887, 3547, 3548, 4208, 4209, 4869, 4870, 5530, 5531, 6191, 6192, ...].
2: Expected 6 pieces. Missing pieces filled with `NA` in 1012 rows [74, 93, 94, 223, 224, 225, 226, 227, 228, 229, 230, 469, 470, 532, 533, 535, 536, 540, 580, 593, ...].
3: Values are not uniquely identified; output will contain list-cols.
* Use `values_fn = list` to suppress this warning.
* Use `values_fn = length` to identify where the duplicates arise
* Use `values_fn = {summary_fun}` to summarise duplicates
有人对这些错误消息有什么建议吗?
【问题讨论】:
-
您可以使用 gsub 轻松摆脱“11908”和“Stool”。例如
gsub("^11908|Stool$","",colnames(dataset))。然后使用 reshape 转换为长格式。 -
您能否澄清一下 fix_dataset 是否是您想要的输出,或者您是否希望 fix_dataset 仍被转换为长格式,如果是,基于哪些列?
-
fix_dataset 是所需的输出,是的
标签: r reshape transpose strsplit csv