Home > other >  Problem of looping over chromosomal files in R
Problem of looping over chromosomal files in R

Time:10-09

This is the command I am running, aim is to go over 22 chromosome files.

for(i in 1:22) {
id <- i
dat <- read.table("~/path/to/files/chromosome",id[i],".QC.het" header=T) # Read in the EUR.het file, specify it has header
m <- mean(dat$F) # Calculate the mean  
s <- sd(dat$F) # Calculate the SD
valid <- subset(dat, F <= m 3*s & F >= m-3*s) # Get any samples with F coefficient within 3 SD of the population mean
write.table(valid[,c(1,2)], "~/path/to/files/chromosome",id[i],".valid.sample", quote=F, row.names=F) # print FID and IID for valid samples
}

I am getting the following error.

Error: unexpected symbol in:
"  id <- i
dat <- read.table("~/analysis_files/ukb_imp_chr",id[i],".QC.het" header"
Execution halted

Any suggestions?

CodePudding user response:

You need to paste your file name:

paste0("~/path/to/files/chromosome",id[i],".QC.het")

CodePudding user response:

You may try this loop -

filenames <- paste0("~/path/to/files/chromosome", 1:22, ".QC.het")

for( i in seq_along(filenames)) {
  dat <- read.table(filenames[i], header=T) 
  m <- mean(dat$F, na.rm = TRUE) 
  s <- sd(dat$F, na.rm = TRUE) 
  valid <- dat[dat$F <= m 3*s & dat$F >= m-3*s,]
  write.table(valid[,c(1,2)], paste0("~/path/to/files/chromosome",i,".valid.sample"), quote=F, row.names=F) 
}
  • Related