Hi all,
I have a problem to run the script in bash from terminal, linux. Basically, the script calculates the monthly sum for 4th column and the monthly average for 5th column, and exports the result.
I saved the script as samp.sh and used the code: ./samp.sh, but got the error that: No such file or directory. I think the input and output file directories are correct, so how to solve it? Thanks for your help.
#!/usr/bin/env Rscript
########### INPUTS
## input file directory
indir <- "/input directory/"
## output file directory
outdir <- "/output directory/"
## name of the input file
infile <- "samp.txt"
## name of the monthly
outfile <- "samp.monthly.txt"
coln <- 4
# set working directory to the specified folder
setwd(indir)
# read in the aggregated file
print(paste("reading input file: ",indir,infile,sep=""))
# agg <- read.table(infile,head=F,col.names=c('YEAR','MONTH','DAY','value1','value2'))
# infile is a text file and the format is like this
agg = data.frame(YEAR=rep(1985,45),MONTH=c(rep(2,28),rep(3,17)),DAY=c(1:28,1:17),
value1=rnorm(45),value2=rnorm(45)*11-5.6)
##############
library(dplyr)
dfavg <- agg %>% group_by(YEAR,MONTH) %>% summarise_each(funs(mean))
dfsum <- agg %>% group_by(YEAR,MONTH) %>% summarise_each(funs(sum))
dffinal <- dfavg
dffinal[,coln] <- dfsum[,coln]
write.table(dffinal, file = outfile,col.names =F,row.names=F,sep="\t")
Because infile is a text file that has 5 columns and 45 rows for daily data, so I manually created the file to match samp.txt.