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 )
}