简体   繁体   English

R:如何根据另一列的条件在空列中设置值?

[英]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.

相关问题 在 R 中,如何根据条件将列中的值替换为另一个数据集的另一列的值? - In R, how to replace values in a column with values of another column of another data set based on a condition? 如何根据R中另一列中的值设置列值 - How to set a column value based on values in another column in R R - 如何根据另一列中值相等的条件为新列选择值 - R - How to choose values for new column based on condition that values are equal in another column 如何根据R中的条件替换列值 - How to replace column values based on a condition in R 如何使用R中的if和non-if函数基于另一列中的条件在新列中分配值 - How to assign values in new column based on condition in another column using if and non-if functions in R 如何根据R中另一列的条件过滤列 - How to filter a column based on a condition from another column in R 根据R中另一列的条件求和 - Sum a column based on condition in another column in R 根据条件从R中的另一个数据表中提取列值 - Extracting column values based on condition from another data table in R 根据另一列中的条件替换列中的值 - Replacing values in a column based on a condition in another column 如何基于R中另一列中的值替换列值? - How to replace column values based on values in another column in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM