简体   繁体   English

移除向量中大于值的元素

[英]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.

相关问题 在不使用循环的情况下,向量中有多少元素大于 x - How many elements in a vector are greater than x without using a loop 在向量中查找大于 X 的第一个值的位置 - Find position of first value greater than X in a vector 用于查找值是否大于向量中的所有先前值的函数 - Function to find if a value is greater than all prior values in a vector 返回 NA 大于设定值的名称向量 - Return vector of names where NA is greater than a set value R:根据矩阵值移除向量元素 - R: Remove vector elements based on matrix value R 如何使用 case_when() 来确定列中的前一个值是否大于有序向量中的前一个值 - R how to use case_when() to determine if previous value in a column is greater than the proceeding value in an ordered vector 从R中的两个向量中,找到第二个向量中的min大于第一个中的每个值 - From two vectors in R, find the min in the second vector greater than each value in the first R Data.table 基于大于值的列向量过滤行 - R Data.table filtering rows based a vector of columns greater than value 在Rstudio中省略大于常数值的值时查找向量的均值 - Finding the mean of a vector while omitting values greater than a constant value in Rstudio 奇怪的大于逻辑与字符向量中的数字 - Strange Greater Than Logic with Numbers in Character Vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM