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

问题描述 投票:0回答:1

当我运行此代码时,我希望它更改数据集中行超过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 DatasetBasic Output

r azure-machine-learning-studio
1个回答
0
投票

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

x <- 1

samp_fun1 <- function(){

  x <- 2  

}

samp_fun2 <- function(){

  x <<- 3

}


samp_fun1() #x still = 1
samp_fun2() #x should equal 3
© www.soinside.com 2019 - 2024. All rights reserved.