我想转换这些数据:
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软件中做到吗?非常感谢。
答案 0 :(得分: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