#### Writing a function with a loop when threshold is met

``````threshold1
[1] 16 22 31  6 11 13

threshold2
[1] 200.0 275.0 387.5  75.0 137.5 162.5
``````

``````column1<-ifelse(data\$column1<threshold1[1],"speeder",
ifelse(data\$column1>threshold2[1], "slower",1))

column2<-ifelse(data\$column2<threshold1[2],"speeder",
ifelse(data\$column2>threshold2[2], "slower",1))

column3<-ifelse(data\$column3<threshold1[3],"speeder",
ifelse(data\$column3>threshold2[3], "slower",1))
``````

2 个回复

``````dat <- data.frame(
column1 = runif(n = 638, min=0, max=220),
column2 = runif(n = 638, min=0, max=300),
column3 = runif(n = 638, min=0, max=400),
column4 = runif(n = 638, min=0, max=100),
column5 = runif(n = 638, min=0, max=150),
column6 = runif(n = 638, min=0, max=200))

# define thresholds
threshold1 <- c(16, 22, 31, 6, 11, 13)
threshold2 <- c(200.0, 275.0, 387.5, 75.0, 137.5, 162.5)
``````

``````# Declare a list that will contain the results
results <- list()

# Loop over the columns
for(i in seq_len(ncol(dat))) {
results[[colnames(dat)[i]]] <- ifelse(dat[,i] < threshold1[i],
yes = "speeder",
no = ifelse(dat[,i] > threshold2[i],
yes = "slower", no = 1))
}
``````

``````results <- lapply(1:ncol(dat), function(x) {
ifelse(dat[,x] < threshold1[x],
yes = "speeder",
no = ifelse(dat[,x] > threshold2[x],
yes = "slower", no = 1))
})

names(results) <- colnames(dat)
``````

``````> head(results\$column1, 100)

[1] "1"       "1"       "1"       "1"       "1"       "1"       "slower"
[8] "1"       "slower"  "1"       "1"       "1"       "speeder" "1"
[15] "1"       "1"       "1"       "1"       "1"       "1"       "1"
[22] "slower"  "1"       "1"       "1"       "1"       "1"       "1"
[29] "1"       "1"       "1"       "slower"  "1"       "slower"  "slower"
[36] "1"       "1"       "1"       "1"       "speeder" "1"       "1"
[43] "1"       "1"       "speeder" "speeder" "1"       "1"       "slower"
[50] "1"       "1"       "slower"  "1"       "1"       "1"       "1"
[57] "1"       "1"       "1"       "1"       "1"       "1"       "1"
[64] "1"       "1"       "1"       "1"       "slower"  "1"       "1"
[71] "slower"  "1"       "1"       "1"       "speeder" "1"       "1"
[78] "1"       "1"       "1"       "1"       "slower"  "1"       "1"
[85] "1"       "1"       "1"       "1"       "1"       "1"       "1"
[92] "1"       "1"       "1"       "1"       "1"       "1"       "1"
[99] "speeder" "1"
``````

``````dat <- data.frame(
column1 = runif(n = 638, min=0, max=220),
column2 = runif(n = 638, min=0, max=300),
column3 = runif(n = 638, min=0, max=400),
column4 = runif(n = 638, min=0, max=100),
column5 = runif(n = 638, min=0, max=150),
column6 = runif(n = 638, min=0, max=200))

# define thresholds
threshold1 <- c(16, 22, 31, 6, 11, 13)
threshold2 <- c(200.0, 275.0, 387.5, 75.0, 137.5, 162.5)

result = matrix(unlist(lapply(seq(6), function(i){
ifelse(dat[,i] < threshold1[i],
yes = "speeder",
no = ifelse(dat[,i] > threshold2[i],
yes = "slower", no = 1))
})), ncol = 6, byrow = FALSE)

[,1]      [,2] [,3] [,4]     [,5] [,6]
[1,] "speeder" "1"  "1"  "slower" "1"  "1"
[2,] "1"       "1"  "1"  "1"      "1"  "1"
[3,] "1"       "1"  "1"  "1"      "1"  "1"
[4,] "1"       "1"  "1"  "slower" "1"  "1"
[5,] "1"       "1"  "1"  "1"      "1"  "1"
[6,] "1"       "1"  "1"  "slower" "1"  "1"
``````
1 在 R 中编写阈值函数

2 编写云函数时如何避免循环？

3 当函数随着每次迭代而增加时，如何编写for循环？

2013-11-21 23:24:31 3 420   r/ jags
4 如何编写一个循环，直到满足条件为止

2014-03-05 15:15:25 4 2577   r
5 为什么这个函数即使满足条件也不会结束它的循环？

2020-05-23 12:33:36 1 13   time
6 如何使用日期范围编写函数而不是for循环

7 当R中满足列条件时，使用for循环填充矩阵

2018-10-19 17:44:01 3 29   r/ matrix
8 从类型编写满足该类型的函数

2012-12-31 05:16:58 3 143   ml
9 满足我的条件时如何退出循环？

10 条件满足时for循环不会结束