Overlap two (text) data.frames by position [R] 根据位置重叠两个(文本)数据框 [R]

huangapple go评论62阅读模式
英文:

Overlap two (text) data.frames by position [R]

问题

我有这两个表格:

> u1 <- structure(list(s1 = c("", "", "MO;SP", ""), s2 = c("", "", "", 
""), s3 = c("", "MO;SP", "", "")), row.names = c("g1", "g2", 
"g3", "g4"), class = "data.frame")

> u2 <- structure(list(s1 = c("", "", "snv", ""), s2 = c("", "", "", 
""), s3 = c("", "snv", "", "")), row.names = c("g1", "g2", "g3", 
"g4"), class = "data.frame")

> u1; u2
      s1 s2    s3
g1               
g2          MO;SP
g3 MO;SP         
g4               
    s1 s2  s3
g1           
g2        snv
g3 snv       
g4 

我想要最终结果如下:

	s1	s2	s3
g1	
g2			MO;SP;snv
g3	MO;SP;snv	
g4	

我猜测mapply可能是一个选项,但我找不到正确的方法。

英文:

I have this two tables:

> u1 <- structure(list(s1 = c("", "", "MO;SP", ""), s2 = c("", "", "", 
""), s3 = c("", "MO;SP", "", "")), row.names = c("g1", "g2", 
"g3", "g4"), class = "data.frame")

> u2 <- structure(list(s1 = c("", "", "snv", ""), s2 = c("", "", "", 
""), s3 = c("", "snv", "", "")), row.names = c("g1", "g2", "g3", 
"g4"), class = "data.frame")

> u1; u2
      s1 s2    s3
g1               
g2          MO;SP
g3 MO;SP         
g4               
    s1 s2  s3
g1           
g2        snv
g3 snv       
g4 

and I'm looking for a final result like this one:

	s1	s2	s3
g1	
g2			MO;SP;snv
g3	MO;SP;snv	
g4	

I can guess mapply could be an option but I cannot find the correct way.

答案1

得分: 2

也许一个选项是在Map中使用paste,然后使用sub来移除

list2DF(Map(\(x, y) sub("^;$", "", paste(x, y, sep=";")), u1, u2))
#         s1 s2        s3
#1                        
#2              MO;SP;snv
#3 MO;SP;snv             
#4                        
英文:

Maybe an option is to use paste in Map and use sub to remove ;.

list2DF(Map(\(x, y) sub("^;$", "", paste(x, y, sep=";")), u1, u2))
#         s1 s2        s3
#1                       
#2              MO;SP;snv
#3 MO;SP;snv             
#4                       

huangapple
  • 本文由 发表于 2023年6月13日 18:49:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76464119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定