[英]R: How to set values in empty column based on condition of another column?
I am trying to write a function that will check to see if each value in a vector is greater than a certain value, and will then initialize a different empty column to 1 or 0. If the value is greater than the set parameter, then the same index of the empty column will be set to 1. Otherwise if it isn't greater than that number, it will be set to 0. So my function will take in a value, column containing probabilities, and then another column that is empty which will later be made into a column of 1's and 0's.我正在尝试编写一个函数,该函数将检查向量中的每个值是否大于某个值,然后将不同的空列初始化为 1 或 0。如果该值大于设置的参数,则空列的相同索引将设置为 1。否则,如果它不大于该数字,它将设置为 0。所以我的函数将接受一个值,包含概率的列,然后是另一个空列稍后将被制成一列 1 和 0。
I don't get any errors when running the function however that empty column does not get updated.运行该函数时我没有收到任何错误,但是该空列没有得到更新。
I feel like my logic is right but clearly something is not working.我觉得我的逻辑是正确的,但显然有些东西不起作用。
cut_off_prob <- function(x, prob, pred_class) {
for (i in 1:length(prob))
{
if(prob[i] > x)
{
pred_class[i] <- 1
}
else {
pred_class[i] <- 0
}
}
}
There is no need of pred_class
as a function parameter.不需要
pred_class
作为函数参数。 Try this:尝试这个:
cut_off_prob <- function(x, prob) {
pred_class <- c() # initialize as an empty vector
for (i in 1:length(prob)){
if(prob[i] > x){pred_class[i] <- 1}
else {pred_class[i] <- 0}
}
return(pred_class)
}
By the other hand, i strongly recommend you not to use for loops in this case, since as @Gregor Thomas said it's very inefficient.另一方面,我强烈建议您不要在这种情况下使用 for 循环,因为正如@Gregor Thomas 所说,它的效率非常低。 In comparison, check this:
相比之下,请检查:
pred_class <- rep(0,length(prob));
pred_class[prob>x] <- 1
Running time is shown here (in seconds):此处显示运行时间(以秒为单位):
"Loop Time: 0.0029909610748291 Conditional Indexing Time: 0.000996828079223633"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.