英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论