Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用for循环创建间隔_R_For Loop_Cut - Fatal编程技术网

使用for循环创建间隔

使用for循环创建间隔,r,for-loop,cut,R,For Loop,Cut,我们可以使用cut进一步创建间隔数据作为标签,也可以使用for循环 割函数 perform_vec <- perform[["Performance"]] perf_labs <- c(1,2,3,4,5,6) perfcut <- cut(perform_vec,breaks = c(0,25,49.99,69.99,84.99,94.99,101),right = F, labels = perf_labs) perfcut_df <- as.d

我们可以使用cut进一步创建间隔数据作为标签,也可以使用for循环

割函数

perform_vec <- perform[["Performance"]]
perf_labs <- c(1,2,3,4,5,6)
perfcut <- cut(perform_vec,breaks = c(0,25,49.99,69.99,84.99,94.99,101),right = F, labels = perf_labs)
perfcut_df <- as.data.frame(cbind(perform_vec, perfcut))
perfcut_df$perfcut <- factor(perfcut_df$perfcut, levels = c(1,2,3,4,5,6), labels = c("D","C","B-","B","A-","A"))
perform_vec在解决方案#1中,我们在一个循环中计算每个输入值超过的
中断次数
,然后使用该数值索引到
标签
。解决方案#2执行相同的操作,但使用
外部
而不是循环。解决方案#3使用
cut

# input
x <- c(30, 50, 80)
breaks <- c(0,25,49.99,69.99,84.99,94.99,101)
labels <- c("D","C","B-","B","A-","A")

# solutions

# 1 - loop
lev <- 0 * x
for (b in breaks) lev <- lev + (x > b)
factor(labels[lev], levels = labels)
## [1] C  B- B 
## Levels: D C B- B A- A

# 2 - outer
factor(labels[rowSums(outer(x, breaks, `>`))], levels = labels)
## [1] C  B- B 
## Levels: D C B- B A- A

# 3 - cut
cut(x, breaks, labels)
## [1] C  B- B 
## Levels: D C B- B A- A
#输入
在解决方案1中,我们在一个循环中计算每个输入值超过多少个
中断
,然后用它索引到
标签
。解决方案#2执行相同的操作,但使用
外部
而不是循环。解决方案#3使用
cut

# input
x <- c(30, 50, 80)
breaks <- c(0,25,49.99,69.99,84.99,94.99,101)
labels <- c("D","C","B-","B","A-","A")

# solutions

# 1 - loop
lev <- 0 * x
for (b in breaks) lev <- lev + (x > b)
factor(labels[lev], levels = labels)
## [1] C  B- B 
## Levels: D C B- B A- A

# 2 - outer
factor(labels[rowSums(outer(x, breaks, `>`))], levels = labels)
## [1] C  B- B 
## Levels: D C B- B A- A

# 3 - cut
cut(x, breaks, labels)
## [1] C  B- B 
## Levels: D C B- B A- A
#输入

x如果它与
cut
一起正常工作,那么为什么要在循环中创建它?如果您看到
cut
的源代码,文件
src/library/base/R/cut.R
,您将看到它调用
C
函数,
bincode
,可以在
src/main/util.C
中找到该函数。你想更有效吗?不在纯
R
中。请不要将图像用于输入,因为这意味着没有人可以在不重新键入的情况下在代码中使用该输入。请通过在问题中显示
dput(x)
的输出将其显示为R代码,其中
x
是输入。如果它与
cut
正常工作,那么为什么要在循环中创建它?如果您看到
cut
的源代码,文件
src/library/base/R/cut.R
,您将看到它调用一个
C
函数,
bincode
,该函数可以在
src/main/util.C
中找到。你想更有效吗?不在纯
R
中。请不要将图像用于输入,因为这意味着没有人可以在不重新键入的情况下在代码中使用该输入。请通过在问题中显示
x
是输入的
dput(x)
的输出,将其显示为R代码。