I am trying to iterate a function I have written in R (strandcode.txt) over all of the files in a given directory.
strandcode.txt is shown below, it's a simple function to compute a Chi Squared test.
strand <- function(file){
data <- as.data.frame(read.table(file))
colnames(data) <- c('chr', 'pos', 'fwd', 'bkwd')
data$chi <- ((.5 - (data$fwd / (data$fwd + data$bkwd)))^2)/.5
keep <- data[data$chi < .823, ]
return(keep)
}
strand{$i}
When I am running this on my Linux server I am using Rscript and iterating over all of the files in the directory by the command below.
for i in $( ls ); do Rscript strandcode.txt >> strandout.txt; done
However this is giving me the error Error: unexpected '{' in "strand{" Execution halted
I have also tried the following command lines (taking the final line out of strandcode.txt)
for i in $( ls ); do Rscript strandcode.txt; Rscript strand{$i} >> strandout.txt; done
for i in $( ls ); do Rscript strandcode.txt strand{$i} >> strandout.txt; done
Both run without an error and without outputting anything to my outfile.
Any suggestions would be greatly appreciated. Thanks!