Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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
如何在R中忽略缺少的值和循环中的NAs?_R_Try Catch_Lapply_Na - Fatal编程技术网

如何在R中忽略缺少的值和循环中的NAs?

如何在R中忽略缺少的值和循环中的NAs?,r,try-catch,lapply,na,R,Try Catch,Lapply,Na,我构建了一个相当长的脚本来遍历CSV文件列表,并为每个CSV文件执行一些函数(如lm()函数),随后我希望在每一轮中将结果作为CSV文件中的一个新列进行保护 现在的问题是,存在NA值,无论这些值在哪里,都应该跳过CSV文件。 现在我读到可以使用tryCatch。我试了很多方法让它工作,但我错过了一些东西。我得到这个错误:tryCatch中的错误({:object'slope\u CC'未找到 您可以在一个tryCatch函数中放入多个函数吗? 我还对如何调用整个过程感到困惑,因为首先,代码为一个

我构建了一个相当长的脚本来遍历CSV文件列表,并为每个CSV文件执行一些函数(如lm()函数),随后我希望在每一轮中将结果作为CSV文件中的一个新列进行保护

现在的问题是,存在NA值,无论这些值在哪里,都应该跳过CSV文件。 现在我读到可以使用
tryCatch
。我试了很多方法让它工作,但我错过了一些东西。我得到这个错误:
tryCatch中的错误({:object'slope\u CC'未找到

您可以在一个tryCatch函数中放入多个函数吗?
我还对如何调用整个过程感到困惑,因为首先,代码为一个CSV文件运行,然后应该为
file\u路径中的CSV文件列表运行。但是这是由脚本末尾的lappy函数完成的吗?
slope\u list如果整个CSV中有任何NAs?
if(anyNA(df))return(something)
我从以前版本的脚本中知道,我在第一个公式中使用了tryCatch,其中有NA值。但我需要脚本忽略这些值
setwd("E:/PYTHON_ST/breakCSV_PYTHON/AIM_2_regions/Test")
csv_path <- "E:\\PYTHON_ST\\breakCSV_PYTHON\\AIM_2_regions\\Test\\selected0.csv"

NDVI_CC <- function(csv_path) {  
  
  df <- read.csv(csv_path, header = TRUE)

  ...  (some data splitting and selecting)
  
  #a) use tryCatch to continue when errors occur
  slope_CC <- tryCatch({
    
  #b) do mutlipe linear regression  
  model <- lm(NDVI ~ T + Prec + soilM, data = BeforeConf)
  #c) use coefficients of MLR in new fomular to calc. new NDVI values
  NDVI_AfterConf_Climate <- summary(model)$coefficients[1,1] + ((summary(model)$coefficients[2,1])*AfterConf$T) +  ((summary(model)$coefficients[3,1])*AfterConf$Prec) + ((summary(model)$coefficients[4,1])*AfterConf$soilM)
  #d) create new table and put years and old and new NDVI values in it
  Afterconf_update <- cbind(AfterConf$NDVI_Year, AfterConf$NDVI, NDVI_AfterConf_Climate)
  
  #d) Calculate NDVi slopes for climate control
  # x = years (after conf)
  # y = NDVI_AfterConf_Climate
  lm_func_CC <- lm(AfterConf$NDVI_Year ~ Afterconf_update[ ,3]) # select the 6th column -> NDVI_AfterConf_Climate should be added as a column in step before
  # Get coefficient:
  tmp_coef <- coef(lm_func_CC)
  # Store coefficient:
  slope_CC <- tmp_coef[2]   

  
  #e) Calculate NDVi slopes for normal NDVI
  # x = years (after conf)
  # y = NDVI_AfterConf
  lm_func_old <- lm(AfterConf$NDVI_Year ~ Afterconf_update[ ,2]) # select the 6th column -> NDVI_AfterConf_Climate should be added as a column in step before
  # Get coefficient:
  tmp_coef <- coef(lm_func_old)
  # Store coefficient:
  slope_old <- tmp_coef[2]  
  
    },
    error = function(e){ 

      print(e)
      return(NA)
    },
    warning = function(w){

      print(w)
      return(Warn)
    },
  return(slope_CC)
  )
#return(data_frame(cbind(slope_CC)))
#return(data_frame(cbind(slope_old)))
}
Slopes_CC <- lapply(csv_path, NDVI_CC)
mypath <- "E:\\PYTHON_ST\\breakCSV_PYTHON\\AIM_2_regions\\Test"
file_paths <- list.files(pattern=".csv", path=mypath)

# LIST RETURN
slope_list <- lapply(file_paths, NDVI_CC)