R不会为变量分配值,但不会引发错误

时间:2019-12-09 19:33:42

标签: r azure-machine-learning-studio

当我运行此代码时,我希望它更改数据集中行的概率不超过60%的行更改为未分配。我正在以正确的方式访问变量,因为您可以看到它显示在输出中,问题在于分配变量。

rowsCat <- array(get('Email Category', envir = as.environment(dataset2) , mode = "any", inherits = TRUE))
rowsCapCallProb <- array(get('Scored Probabilities for Class "Capital Call"', envir = as.environment(dataset1) , mode = "any", inherits = TRUE))
rowsDistroProb <- array(get('Scored Probabilities for Class "distribution"', envir = as.environment(dataset1) , mode = "any", inherits = TRUE))
rowsFinnancialProb <- array(get('Scored Probabilities for Class "Finnancial Document"', envir = as.environment(dataset1) , mode = "any", inherits = TRUE))

data.set = rbind(dataset1)

checkProb <- function(count) {
  print("===================================================================")
  print(paste("Evaluating Probabilities on Row: ", count))


  #Check to see if Probabilities are over 60%
  if(rowsCapCallProb[[count]] > .60 | rowsDistroProb[[count]] > .60 | rowsFinnancialProb[[count]] > .60){
    print(paste("A probable Score was located for Row: ",count))
  } else {
    print(paste("Attempting to assign 'Unassigned' to Row: ",count, " : ", data.set$ScoredLables[[count]]))

    #\/ Issue is here in the assignment \/
    data.set$ScoredLables[[count]] <- "Unassigned"    
  }

  print("===================================================================")
}


for (rowCat in rowsCat) {
    checkProb(count)

    count = count+1
}

Output Dataset Basic Output

1 个答案:

答案 0 :(得分:-2)

检查函数内部的作用域。如果要从函数内部将变量(覆盖)写入全局环境,请使用<<-,而不要使用<-。请参见以下示例:

x <- 1

samp_fun1 <- function(){

  x <- 2  

}

samp_fun2 <- function(){

  x <<- 3

}


samp_fun1() #x still = 1
samp_fun2() #x should equal 3