R:将矩阵中的元素替换为与另一个矩阵中的NA对应的位置 [英] R: Replace elements with NA in a matrix in corresponding positions to NA's in another matrix

查看:41
本文介绍了R:将矩阵中的元素替换为与另一个矩阵中的NA对应的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大矩阵 z,我删除了所有 >3 的值并用 NA 替换:

I have a large matrix, z, that I removed all values >3 and replaced with NA using:

z[z>3]<-NA

我有另一个矩阵,y,我需要用 NA 替换与元素 z 中元素被替换的位置相对应的位置的值.也就是说,如果 z[3,12] 为 >3 并替换为 NA,我也需要将 y[3,12] 替换为 NA.如果有帮助,它们具有相同的行名.

I have another matrix, y, of identical dimensions that I need to replace values with NA in positions corresponding to the locations where the elements were replaced in element z. That is, if z[3,12] was >3 and replaced with NA, I need y[3,12] to be replaced with NA too. They have the same row names if that helps.

推荐答案

怎么样:

is.na(y) <- is.na(z) <- z < 3 & !is.na(z)

或者干脆

is.na(y) <- is.na(z) <- z < 3

如果z在赋值之前保证没有缺失值

if z is guaranteed not to have missing values prior to the assignment

这篇关于R:将矩阵中的元素替换为与另一个矩阵中的NA对应的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆