R 在特定行中隐藏多行
我想转换这些数据:R 在特定行中隐藏多行,r,plyr,reshape,tapply,R,Plyr,Reshape,Tapply,我想转换这些数据: Sample Genotype Region sample1 A Region1 sample1 B Region1 sample1 A Region1 sample2 A Region1 sample2 A Region1 sample3 A Region1 sample4 B Region1
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软件中执行?非常感谢 一种简单的方法是使用
aggregate
。假设您的data.frame
名为“mydf”(并基于Jorg的评论):
许多选项:
tapply
或ddply
来自plyr软件包或数据。表
package我想在一个统一的行中,将“基因型”列中排除的(E)标记为具有多个基因型的样本(样本1),并将行统一为具有两行重复基因型的样本(样本2),这是一个数据框吗,单列是因子还是带字符串的矩阵?这个分析是按地区进行的吗?或者如何总结。对于基因型,您可以使用类似函数(x)ifelse(长度(唯一(x))==1,x[1],'E')
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