【问题标题】:How to split column names and drop parts of the names and convert data from wide to long format in R如何在R中拆分列名并删除部分名称并将数据从宽格式转换为长格式
【发布时间】: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


【解决方案1】:

您可以使用以下代码实现此目的:

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("taxa"), timept1)

给出:

# A tibble: 4 x 6
  study_id individual taxa1 taxa2 taxa3 timept1
  <chr>    <chr>      <dbl> <dbl> <dbl> <chr>  
1 0008     Inf            0  1760     0 6m     
2 01115    Inf            0  1517     0 6m     
3 0044     Inf            0 10815     0 6m     
4 0125     Mom            0  4719     0 6m 

请注意,您的学习 ID 存在一些不一致之处,即在您的原始数据集中,其中一个 ID 是“01115”,而在您的首选输出中,它是“0115”。

【讨论】:

  • 是的,这是我在输入示例数据集时出错
  • 如果解决方案适合您,您可以通过单击左侧的小复选标记来选择答案作为已接受的答案。
  • 您能否提供一个更详尽的代码示例,其中还包含一些不符合当前命名方案的其他列?
猜你喜欢
  • 2014-10-05
  • 1970-01-01
  • 1970-01-01
  • 2023-02-25
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 2020-09-18
  • 2013-10-22
相关资源
最近更新 更多