Dear R experts,

Might be very simple question to ask but would be insightful. As the same story of nested "for loops". following is the code that I am using to get the autocorrelation function of the sample data. I have tried to get rid of for loops but since I am touching R after such a long time that I need to practice more but I need help to revive my skills. I know that apply() or sapply() would be beneficial. I need help...

Best Wishes,
Baloo 

R code
----------
acf_wal <- function (infile) {
    data<-read.table(infile)
    data_value <- data[,-1]
    data_value_mean <- mean(data_value)
    data_value_square <- (data_value - data_value_mean) ^ 2 
    square_sum<-sum(data_value_square)
    entry<-NROW(data_value)
    deno<-square_sum/entry

    tab1<-c()
    tab2<-c()
    ps_value <- seq(0,(floor(entry/2)),1)
    
    for(k in 0:(floor(entry/2))){
        for (i in 1:(entry-k)) {
            mult<-(data_value[i] - data_value_mean) * (data_value [i+k] - data_value_mean)
            tab1 <- c(tab1,mult)
        }
            auto_avg<-mean(tab1)
            tab1<-c()
            auto_corr<-auto_avg/deno
            tab2<-c(tab2,auto_corr)
    }

    table_value <- cbind (ps_value, tab2)
    colnames(table_value) <- c("#ps", "acf")


    outfile<-unlist(strsplit(infile, split=".", fixed=TRUE))[1]

    write.table(table_value,file=paste(outfile,"acf.dat",sep="-"),row.names=FALSE,sep="\t",quote=F)
}

--------------

Sample data
------------------

1 16.0071
2 16.7966
3 17.575
4 18.1614
5 15.982
6 16.8515
7 15.6828
8 14.9652
9 14.8623
10 14.7079

--------------------

Help in this regard would be highly appreciated.

 



      
	[[alternative HTML version deleted]]


