选择的未定义列,无法XTFRM数据框架错误

发布于 2025-02-08 20:36:50 字数 718 浏览 2 评论 0原文

我正在尝试编写一个基于IQR检查异常值的代码,并将这些值更改为“ NA”。因此,我写了这篇文章:

dt <- rnorm(200) 
dg <- rnorm(200)
dh <- rnorm(200)

l <- c(1,3) #List of relevant columns

df <- data.frame(dt,dg,dh)

要检查列是否包含任何异常值并将其值更改为NA:

vector.is.empty <- function(x) return(length(x) ==0) 
#Checks for empty values in vector and returns booleans. 

for (i in 1:length(l)){
 IDX <- l[i]
 BP <- boxplot.stats(df[IDX])
 OutIDX <- which(df[IDX] %in% BP$out)
 if (vector.is.empty(OutIDX)==FALSE){
  for (u in 1:length(OutIDX)){
      IDX2 <- OutIDX[u]
      df[IDX2,IDX] <- NA
    }
  }
}

因此,当我运行此代码时,我会收到这些错误消息:

​任何好的答案。但是我不确定为什么他们声称该列未指定。这里有线索吗?

I am trying to write a code that checks for outliers based on IQR and change those respective values to "NA". So I wrote this:

dt <- rnorm(200) 
dg <- rnorm(200)
dh <- rnorm(200)

l <- c(1,3) #List of relevant columns

df <- data.frame(dt,dg,dh)

To check if the column contains any outliers and change their value to NA:

vector.is.empty <- function(x) return(length(x) ==0) 
#Checks for empty values in vector and returns booleans. 

for (i in 1:length(l)){
 IDX <- l[i]
 BP <- boxplot.stats(df[IDX])
 OutIDX <- which(df[IDX] %in% BP$out)
 if (vector.is.empty(OutIDX)==FALSE){
  for (u in 1:length(OutIDX)){
      IDX2 <- OutIDX[u]
      df[IDX2,IDX] <- NA
    }
  }
}

So, when I run this code, I get these error messages:

enter image description here

I've tried to search online for any good answers. but I'm not sure why they claim that the column is unspecified. Any clues here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘你却要生生世世 2025-02-15 20:36:51

我会做类似的事情以替换异常值:

# Set a seed (to make the example reproducible)
  set.seed(31415)
# Generate the data.frame
  df <- data.frame(dt = rnorm(100), dg = rnorm(100), dh = rnorm(100))
# A list to save the result of boxplot.stats()
  l <- list()
  for (i in 1:ncol(df)){
    l[[i]] <- boxplot.stats(df[,i])
    df[which(df[,i]==l[[i]]$out),i] <- NA
  }

# Which values have been replaced?
  lapply(l, function(x) x$out)

I would do something like that in order to replace the outliers:

# Set a seed (to make the example reproducible)
  set.seed(31415)
# Generate the data.frame
  df <- data.frame(dt = rnorm(100), dg = rnorm(100), dh = rnorm(100))
# A list to save the result of boxplot.stats()
  l <- list()
  for (i in 1:ncol(df)){
    l[[i]] <- boxplot.stats(df[,i])
    df[which(df[,i]==l[[i]]$out),i] <- NA
  }

# Which values have been replaced?
  lapply(l, function(x) x$out)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文