[英]Remove elements in a vector that are greater than value
I need to remove elements (that I am plotting with error bars, so I need to remove that data point from all four vectors below) where the diff
vector is greater than 2*std. 我需要删除diff
矢量大于2 * std的元素(我正在使用误差线进行绘制,因此需要从下面的所有四个矢量中删除该数据点)。 Here was my thought: 这是我的想法:
for (i in 1:length(Z)){
if (diff[[i]]>=(2*std)){
Z[[i]] <- NULL
ucl[[i]] <- NULL
lcl[[i]] <- NULL
x[[i]] <- NULL
}
}
The for loop stops completely after it enters the if statement for the first time. for循环在第一次进入if语句后完全停止。 I have learned R completely on my own, so please respond to me as if I know next to nothing. 我已经完全独立学习了R,因此请回覆我,好像我什么都不知道。
Don't use a for loop. 不要使用for循环。 Do something like this if you want to replace certain values by NA: 如果要用NA替换某些值,请执行以下操作:
Z[diff >= 2*std] = NA
Alternatively, if you want to just filter out the rows that don't satisfy the condition, subset only the rows you want: 另外,如果您只想过滤出不满足条件的行,则仅对所需的行进行子集化:
Z <- Z[diff < 2*std]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.