R循环分位数除非

R loop quantiles unless

我有一个数据框 DF,

DF <- data.frame(Column1 = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                             1,1,1,1,1,1,1,1,1,2,3))

q1 <-quantile(na.omit(DF$Column1), 0.25 ,names = FALSE)
q3 <- quantile(na.omit(DF$Column1), 0.75 ,names = FALSE)


DF <- data.frame(Column1 = c(1,1,1,1,1,1,1,1,1,2,3))

q1 <-quantile(na.omit(DF$Column1), 0.25 ,names = FALSE)
q3 <- quantile(na.omit(DF$Column1), 0.75 ,names = FALSE)

我想要这样的功能:

If Quantile1=Quantile3=0 
then DF$Column1 = DF$Column1 != 0 else DF$Column1 (remove that 0)
then we removed 0 and there is another case again Quantile1=Quantile3=1
then DF$Column1 = DF$Column1 != 1 else DF$Column1 (remove that 1)

我希望此循环 运行 除非 Column1 值中没有 Quantile1=Quantile3。

我该如何编写该循环?

谢谢。

考虑这样的函数

neq_quantile <- function(x, q = c(0.25, 0.75), na.rm = TRUE) {
  na_pos <- which(is.na(x))
  y <- x
  x_pos <- seq_along(x)
  if (length(na_pos) > 0L) {
    y <- y[-na_pos]
    x_pos <- x_pos[-na_pos]
  }
  while(length(y) > 0L && all(diff(rng <- quantile(y, q)) == 0)) {
    keep <- y != rng[[1L]]
    y <- y[keep]
    x_pos <- x_pos[keep]
  }
  out <- logical(length(x))
  out[x_pos] <- TRUE
  if (!na.rm) out[na_pos] <- NA
  out
}

那你就可以

DF <- data.frame(
  Column1 = c(NA, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,2,3), 
  Column2 = 1:46
)
DF[neq_quantile(DF$Column1), ]

输出

   Column1 Column2
45       2      45
46       3      46

如果na.rm = FALSE,NA 将保持原样。例如,

> neq_quantile(DF$Column1)
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE
> neq_quantile(DF$Column1, na.rm = FALSE)
 [1]    NA FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE