R语言 如果特定列中有两个或多个字符串,则向数据框添加行

64jmpszr  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(147)

我的数据看起来是这样的:

> dput(dd)
structure(list(TestGroups = c("P61294;Q0PD53", "O55143", "P16330;Q3TYV5", 
"P61979;Q3TUA1;Q5FWJ5", "P68037;Q561N4", "H3BL37;O08784;Q05BH6;Q7TPZ2", 
"A0A0R4IZX5;P55066", "P27601;Q3UE40", "P11930", "Q9QXS6", "Q9D0K2"
), Ids = c("P61294;Q0PD53", "O55143", "P16330;Q3TYV5", "P61979;Q3TUA1;Q5FWJ5", 
"P68037;Q561N4", "H3BL37;O08784;Q05BH6;Q7TPZ2", "A0A0R4IZX5;P55066", 
"P27601;Q3UE40", "P11930", "Q9QXS6", "Q9D0K2"), Test.Name = c("RAB6B_ART;Q0PD53_ART", 
"AT2A2_ART", "CN37_ART;Q3TYV5_ART", "HNRPK_ART;Q3TUA1_ART;Q5FWJ5_ART", 
"UB2L3_ART;Q561N4_ART", "H3BL37_ART;TCOF_ART;Q05BH6_ART;Q7TPZ2_ART", 
"A0A0R4IZX5_ART;NCAN_ART", "GNA13_ART;Q3UE40_ART", "NUD19_ART", 
"DREB_ART", "SCOT1_ART"), TestDescription = c("Mark", "Barry", 
"Dirk", "Greg", "Tom", "Tom", "Jerry", "Orfa", "Morso", "Orx", 
"Tesra"), Factors = c("Rab6b", "Atp2a2", "Cnp", "Hnrnpk", "Ube2l3", 
"Tcof1", "Ncan", "Gna13", "Nudt19", "Dbn1", "Oxct1"), Removed_part = c("TDLADKR", 
"DACLNAR", "AAARK", "HESGASIK", "NAEEFTK", "TGPAATQAK", "VQTASGQR", 
"TDLLEEK", "DTTTFLEK", "TDAAVEMK", "SGKPGGDVR")), row.names = c(62L, 
72L, 80L, 96L, 116L, 128L, 139L, 154L, 163L, 172L, 189L), class = "data.frame")

我想通过添加一个额外的行来分隔TestGroups列中的字符串。例如,让我们以第一行为例:

P61294;Q0PD53               P61294;Q0PD53                      RAB6B_ART;Q0PD53_ART            Mark   Rab6b      TDLADKR

这就是我想要的输出结果:

P61294               P61294;Q0PD53                      RAB6B_ART;Q0PD53_ART            Mark   Rab6b      TDLADKR
 Q0PD53               P61294;Q0PD53                      RAB6B_ART;Q0PD53_ART            Mark   Rab6b      TDLADKR

正如你所看到的,只有第一列的字符串被提取并分隔并放在不同的行中,其余的列只是被复制/复制。也可能发生在TestGroups列中有超过2个字符串。请注意,我想删除分隔标记';只保留纯粹的名字

7xllpg7q

7xllpg7q1#

我们可以使用separate_longer_delim()

library(dplyr)
library(tidyr)

dd %>% 
  separate_longer_delim(TestGroups, delim = ";") # now recommended
 # separate_rows(TestGroups, sep = ";")
# A tibble: 21 × 6
   TestGroups Ids                  Test.Name                       TestDescription Factors Removed_part
   <chr>      <chr>                <chr>                           <chr>           <chr>   <chr>       
 1 P61294     P61294;Q0PD53        RAB6B_ART;Q0PD53_ART            Mark            Rab6b   TDLADKR     
 2 Q0PD53     P61294;Q0PD53        RAB6B_ART;Q0PD53_ART            Mark            Rab6b   TDLADKR     
 3 O55143     O55143               AT2A2_ART                       Barry           Atp2a2  DACLNAR     
 4 P16330     P16330;Q3TYV5        CN37_ART;Q3TYV5_ART             Dirk            Cnp     AAARK       
 5 Q3TYV5     P16330;Q3TYV5        CN37_ART;Q3TYV5_ART             Dirk            Cnp     AAARK       
 6 P61979     P61979;Q3TUA1;Q5FWJ5 HNRPK_ART;Q3TUA1_ART;Q5FWJ5_ART Greg            Hnrnpk  HESGASIK    
 7 Q3TUA1     P61979;Q3TUA1;Q5FWJ5 HNRPK_ART;Q3TUA1_ART;Q5FWJ5_ART Greg            Hnrnpk  HESGASIK    
 8 Q5FWJ5     P61979;Q3TUA1;Q5FWJ5 HNRPK_ART;Q3TUA1_ART;Q5FWJ5_ART Greg            Hnrnpk  HESGASIK    
 9 P68037     P68037;Q561N4        UB2L3_ART;Q561N4_ART            Tom             Ube2l3  NAEEFTK     
10 Q561N4     P68037;Q561N4        UB2L3_ART;Q561N4_ART            Tom             Ube2l3  NAEEFTK     
# … with 11 more rows
# ℹ Use `print(n = ...)` to see more rows

相关问题