使用apply函数或for循环多次执行脚本

使用apply函数或for循环多次执行脚本,r,loops,apply,R,Loops,Apply,我想多次运行脚本(然后再做一些其他事情)。数据以以下形式存在于文本文件(名为test.txt)中: A B C D E 1 2 2 1 9 3 5 1 3 0 2 NA 4 13 2 并使用 test <- read.table("test.txt",header=TRUE) eval(expr、envir、enclose)中出错:找不到对象“i” 我找到了一个关于循环索引的讨论,还发现存在(I)在消息正确显示时返回false

我想多次运行脚本(然后再做一些其他事情)。数据以以下形式存在于文本文件(名为
test.txt
)中:

A   B   C   D   E
1   2   2   1   9
3   5   1   3   0
2   NA  4   13  2
并使用

test <- read.table("test.txt",header=TRUE)
eval(expr、envir、enclose)中出错:找不到对象“i”

我找到了一个关于循环索引的讨论,还发现
存在(I)
在消息正确显示时返回false。


我想使用
apply
函数或循环执行
outlier
函数,检查数据所有列中的异常值。

您可以尝试以下操作:

source("https://datascienceplus.com/rscript/outlier.R")

A = c(1, 3, 2, 2, 3)
B = c(2, 5, 0, 1, 6)
C = c(2, 1, 4, 7, 8)
D = c(1, 3, 99, 4, 2)
E = c(9, 0, 2, 8, 4)

df = data.frame(A, B, C, D, E)

x <- 0

for (i in df) {
  x <- x + 1
  names <- names(df)
  message("Variable: ", names[x])
  outlierKD(dt = df, var = i)
}
test <- read.table("test.txt",header=TRUE)
source("outlierKD_mod.r")
source("loopscript.r")
loopscript(test)
源代码(“https://datascienceplus.com/rscript/outlier.R")
A=c(1,3,2,2,3)
B=c(2,5,0,1,6)
C=C(2,1,4,7,8)
D=c(1,3,99,4,2)
E=c(9,0,2,8,4)
df=数据帧(A、B、C、D、E)

x您可以尝试以下方法:

source("https://datascienceplus.com/rscript/outlier.R")

A = c(1, 3, 2, 2, 3)
B = c(2, 5, 0, 1, 6)
C = c(2, 1, 4, 7, 8)
D = c(1, 3, 99, 4, 2)
E = c(9, 0, 2, 8, 4)

df = data.frame(A, B, C, D, E)

x <- 0

for (i in df) {
  x <- x + 1
  names <- names(df)
  message("Variable: ", names[x])
  outlierKD(dt = df, var = i)
}
test <- read.table("test.txt",header=TRUE)
source("outlierKD_mod.r")
source("loopscript.r")
loopscript(test)
源代码(“https://datascienceplus.com/rscript/outlier.R")
A=c(1,3,2,2,3)
B=c(2,5,0,1,6)
C=C(2,1,4,7,8)
D=c(1,3,99,4,2)
E=c(9,0,2,8,4)
df=数据帧(A、B、C、D、E)

在这种情况下,使用for循环更容易

test <- read.table("test.txt", header=TRUE) #copied and pasted
test

source("https://datascienceplus.com/rscript/outlier.R") #function

for(i in 1:ncol(test)) outlierKD(dt=test, i)

test在这种情况下使用for循环更容易

test <- read.table("test.txt", header=TRUE) #copied and pasted
test

source("https://datascienceplus.com/rscript/outlier.R") #function

for(i in 1:ncol(test)) outlierKD(dt=test, i)
test(我想稍微修改一下脚本,并将它存储在一个.r文件中,以及将多次执行它(并执行一些其他操作)的脚本中,如下所示:)

为此:

test[x]=var_name
命令将删除通过异常值检查添加到数据框中的列:

test <- subset(test, select = -c(i) )
test(我想稍微修改一下脚本,并将它存储在一个.r文件中,以及将多次执行它(并执行一些其他操作)的脚本中,如下所示:)

为此:

test[x]=var_name
命令将删除通过异常值检查添加到数据框中的列:

test <- subset(test, select = -c(i) )

测试不幸的是,脚本没有以这种方式“查看”数据:
异常值列表:0来自1个观测值异常值的比例(%):0异常值的平均值:未删除异常值的NaN平均值:1如果我们删除异常值,则平均值:1不幸的是,脚本没有“查看”以这种方式处理数据:
异常值列表:0从1个观测值中选取比例(%)异常值:0异常值平均值:NaN平均值而不删除异常值:1平均值如果我们删除异常值:1
出于某种原因,当脚本必须删除异常值时,原始列保持不变,并创建一个名为
i
的新列,删除异常值的位置。由于某种原因,当脚本必须删除异常值时,原始列保持不变,并创建一个名为
i
的新列,删除异常值。