For循环可在一个文件夹中处理多个csv,并保存新的已处理csvs文件

问题描述

我正在尝试批处理一个文件夹中包含的40个csv文件,并根据其空间位置计算邻居信息。我使用for循环将代码应用于所有csv文件,但最终以某种方式最终仅获得1个输出文件。我想为每个输入csv文件提供一个不同的输出文件文件。我尝试了以下代码无济于事。任何建议将不胜感激!

###  INPUT DATA 
   csv_file_path <- "~/Google Drive/Projects/Projects_MME/Projects/4_EnzColSpa/Microscopy-       SingleCell/Out-Ilastik/Cell-Lists_all/"

    csv_folder_name     <- "TrackList"

# set working directory
   Files <- list.files(paste0(csv_file_path,csv_folder_name),pattern = ".csv") 

    for (f in Files) {
   # for each csv file
  
  # import tracklist input
  csv <- read.delim(file=f,header=TRUE,sep=",",as.is = F)
  
  # 10 neighbours selected 
  n_neighbors <- 10
  {
    
  # make matrix of coordinates
  mat <- as.matrix(csv[,3:4])
  rownames(mat) <- csv$trackId
  
  # compute [euclidian] distances
  dmat <- as.matrix(dist(mat))
  
  # find neighbors (by name)
  nei_mat <- apply(dmat,1,function(crow) {names(sort(crow))[seq_len(n_neighbors+1)]})[-1,]
  
  # match names to initial data frame to make matrix of growth
  ref_growth_mat <- matrix(csv$growth_rate_column,dimnames=list(csv$trackId))
  growth_mat <- matrix(ref_growth_mat[nei_mat,],nrow = n_neighbors)
  colnames(growth_mat) <- csv$trackId
  
  # done
  growth_mat
  
  # done,transpose and export
  growth_matcol <- t(growth_mat)
  }
  
  
    {  #export
  #write.table(growth_matcol,"growth_matcol.csv",sep = ",col.names = T,append = T)
write.table(growth_matcol,paste0(csv_file_path,csv_folder_name,"growth_matcol.csv"),append = T)
  }
  
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)