gpt4 book ai didi

r - pivot_wider() 为每个观察生成多行。重复问题?

转载 作者:行者123 更新时间:2023-12-01 23:57:19 25 4
gpt4 key购买 nike

<分区>

我无法让我的数据框 pivot_wider 到每个观察集的一行中。数据框有成对的观察结果(CopyForward TemplateReverse Template,值在 prim 中)嵌套在加入。我正在尝试 pivot_wider,在最终数据框中为每个配对观察留一行,我需要能够在总结时将 copy 跟踪到 Accession

展望 future ,我还想在每个副本中包含三个观察结果(未包含在此示例中)。

下面的代码为 prim 的每个值生成一个单独的列。

library(tibble)
library(dplyr)
library(tidyr)

df <-
structure(list(prim = c("Template 601881 .................... 601900",
"Template 601973 ...................... 601952", "Template 331595 .................... 331614",
"Template 331687 ...................... 331666", "Template 196557 .................... 196576",
"Template 196649 ...................... 196628", "Template 153933 .................... 153952",
"Template 154025 ...................... 154004", "Template 2100939 .................... 2100920",
"Template 2100847 ...................... 2100868", "Template 11970 .................... 11989",
"Template 12062 ...................... 12041", "Template 2030677 .................... 2030658",
"Template 2030585 ...................... 2030606", "Template 1988028 .................... 1988009",
"Template 1987936 ...................... 1987957", "Template 1850917 .................... 1850898",
"Template 1850825 ...................... 1850846", "Template 1580702 .................... 1580683",
"Template 1580610 ...................... 1580631"),
Accession = c("CP042983.1", "CP042983.1", "CP042983.1", "CP042983.1",
"CP042983.1", "CP042983.1", "CP042983.1", "CP042983.1", "CP042983.1",
"CP042983.1", "CP043001.1", "CP043001.1", "CP043001.1", "CP043001.1",
"CP043001.1", "CP043001.1", "CP043001.1", "CP043001.1", "CP043001.1",
"CP043001.1"), Genus = c("Histophilus", "Histophilus", "Histophilus",
"Histophilus", "Histophilus", "Histophilus", "Histophilus",
"Histophilus", "Histophilus", "Histophilus", "Histophilus",
"Histophilus", "Histophilus", "Histophilus", "Histophilus",
"Histophilus", "Histophilus", "Histophilus", "Histophilus",
"Histophilus"), Species = c("somni", "somni", "somni", "somni",
"somni", "somni", "somni", "somni", "somni", "somni", "somni",
"somni", "somni", "somni", "somni", "somni", "somni", "somni",
"somni", "somni"), Metric = c("Forward Template", "Reverse Template",
"Forward Template", "Reverse Template", "Forward Template",
"Reverse Template", "Forward Template", "Reverse Template",
"Forward Template", "Reverse Template", "Forward Template",
"Reverse Template", "Forward Template", "Reverse Template",
"Forward Template", "Reverse Template", "Forward Template",
"Reverse Template", "Forward Template", "Reverse Template"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-20L))

tmp<-
df %>%
mutate(CopyID = rep(1:1000, each = 2)) %>%
group_by(Accession) %>%
mutate(AccessID = group_indices()) %>%
pivot_wider(names_from = Metric, values_from = prim)

tmp

如何创建一个数据框,其中填充了 Reverse TemplateForward Template 列,并且每个观察只创建一行?

干杯

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com