0

I need to import the 2nd and the 3rd column from a folder of multiple.csv files perform an operation on these data (operation will be called from another separate R code) and write the results in a single excel sheet(either xlsx or .csv). I have given a lot of tries by incorporating various commands, still doesn't work. I am attaching the code below:

library(doParallel)
cl <- makeCluster(2)
registerDoParallel(cl)
library(foreach)
# load SSR functions
source("H:\\Users\\Deep\\Desktop\\SHHS\\SSR_functions.R")

# CCM analysis for 2sp model time

 # load data
 x <- read.table("H:\\Users\\Deep\\Desktop\\SHHS\\rawRR2.txt",fill=TRUE)
 y <- read.table("H:\\Users\\Deep\\Desktop\\SHHS\\rawQT2.txt",fill=TRUE)

 nc <- NCOL(x)

 foreach(i = 97:nc) %dopar% {

 temp_xv=x[,i]
  temp_yv=y[,i]

  xv = temp_xv[is.na(temp_xv)==FALSE]
   yv = temp_yv[is.na(temp_yv)==FALSE]

      F=length(xv)

      lib <- c(1, F)
        pred <- c(1, F)
         lib_sizes <- c(F/100, F/50, F/25, F/12.5, F/6.25, F/3.125, F)
           E <- 3

           x_xmap_y <- ccm(xv, yv, lib_sizes, lib, pred, E)
               y_xmap_x <- ccm(yv, xv, lib_sizes, lib, pred, E)


                 # compute mean rhos at each L

           x_xmap_y$L <- as.factor(x_xmap_y$L)
         x_xmap_y_means <- do.call(rbind, lapply(split(x_xmap_y,x_xmap_y$L), function(x){max(0, mean(x$rho))}))
           y_xmap_x$L <- as.factor(y_xmap_x$L)
           y_xmap_x_means <- do.call(rbind, lapply(split(y_xmap_x,y_xmap_x$L), function(x){max(0, mean(x$rho))}))

       output=cbind(x_xmap_y_means, y_xmap_x_means)

      write.table(output, file=(paste("H:\\Users\\Deep\\Desktop\\SHHS\\output\\m3_",i,".csv" ,sep="")), col.names = FALSE )
          }
DaphFab
  • 81
  • 8
  • `foreach` has a `.verbose` option that is useful to debugging. It's also helpful to use `%do%` (single thread) until the code is working then switch back to `%dopar%`. – manotheshark Apr 06 '17 at 16:57
  • You can [read multiple files](http://stackoverflow.com/questions/32888757/reading-multiple-files-into-r-best-practice/32888918?s=1|0.1100#32888918) in R and [select just the 2nd and 3rd column](http://stackoverflow.com/questions/5788117/only-read-limited-number-of-columns/33201353?s=3|0.1073#33201353) from each and then [use `WriteXLS`](http://stackoverflow.com/questions/35435770/export-descriptive-statistics-row-of-values-into-an-excel-sheet-from-r/35436206#35436206) to write them to an excel file with a separate sheet for each file – Jaap Apr 06 '17 at 17:40

0 Answers0