0

我想转换这些数据:

    Sample  Genotype  Region
    sample1    A      Region1
    sample1    B      Region1
    sample1    A      Region1
    sample2    A      Region1
    sample2    A      Region1
    sample3    A      Region1
    sample4    B      Region1

在该格式中,使用具有多个基因型的“E”样本标记并将具有相同基因型的样本统一 2 次:

    Sample  Genotype  Region   
    sample1    E      Region1
    sample2    A      Region1
    sample3    A      Region1
    sample4    B      Region1

我有一个包含许多区域的列表(Region1 - Regionx)。可以在R软件中做吗?非常感谢。

4

1 回答 1

0

一种直接的方法是使用aggregate. 假设您data.frame的名称为“mydf”(并基于 Jorg 的评论):

aggregate(Genotype ~ ., mydf, function(x) {
  a = unique(x)
  ifelse(length(a) > 1, "E", a) 
})
#    Sample  Region Genotype
# 1 sample1 Region1        E
# 2 sample2 Region1        A
# 3 sample3 Region1        A
# 4 sample4 Region1        B
于 2014-03-27T17:01:35.390 回答