将一行分成两行

Split one row into two

我想将数据帧的每一行一分为二。这是我的输入:

input <- 'name sample1 sample2 sample3
              pr_001  533  411    633
              pr_002  478  447  427'  
input <- read.table(text=input, header=T)

要获得此输出:

output <- 'name sample1 sample2 sample3
              pr_001-A  533  411    633
              pr_001-B  533  411   633
              pr_002-A  478  447  427  
              pr_002-B  478  447  427'
 output <- read.table(text=output, header=T)

因此,对于 sample1 中的 pr_001,结果是具有相同值的两行 pr_001-Apr_001-A,并且必须遵循相同的逻辑样品和名称。一些想法来处理那个?谢谢!

首先复制行(基于):

output <- input[rep(1:nrow(input), each = 2),]

然后将 -A-B 添加到每个 name 中:

output$name <- paste(output$name, c("A", "B"), sep = "-")
library(dplyr)

bind_rows(input %>% 
            mutate(sample1 = paste(sample1, "A", sep = "-"),
          input %>% 
            mutate(sample1 = paste(sample1, "B", sep = "-") ) %>%
  arrange(sample1)