我有一个尺寸低于矩阵的矩阵,矩阵包含一组遗传变体之间的计算距离,我想创建一个新矩阵或将PosDiff矩阵修改为只有小于或等于500,000的距离。
aaa
我已经尝试了dim(PosDiff)
[1] 597 41099
,subset()
并获得了不稳定的结果,例如带有1列和41099观测值的矩阵
谢谢
答案 0 :(得分:0)
好的,我们去吧
# Generate a random matrix with 4 rows and 3 cols
> m <- matrix(runif(12), nrow=4)
> m
# [,1] [,2] [,3]
#[1,] 0.62361346 0.7793682 0.9447203
#[2,] 0.14844661 0.7335280 0.2936238
#[3,] 0.08026447 0.8172304 0.1490721
#[4,] 0.46406955 0.1701625 0.7193786
# Then keep all the elements <= 0.5 setting all the rest to NA
> m1 <- apply(m, FUN=function(x){ifelse(x<=0.5, NA, x)}, MARGIN = c(1,2))
> m1
# [,1] [,2] [,3]
#[1,] NA NA NA
#[2,] 0.14844661 NA 0.2936238
#[3,] 0.08026447 NA 0.1490721
#[4,] 0.46406955 0.1701625 NA
如果您只想要小于0.5
的值,那么您可以运行m[which(m<=0.5)]
答案 1 :(得分:0)
也许你只需要:
ifelse(PosDiff <= 500000., PosDiff, NA)
或:
ifelse(PosDiff <= 500000., PosDiff, 0)
取决于您是想要缺少值还是0而不是大于500000的元素。