Looping through files in R

后端 未结 3 1178
鱼传尺愫
鱼传尺愫 2021-01-25 02:37

I am using R to calculate the mean values of a column in a file like so:

R
file1 = read.table(\"x01\")
mean(file1$V4)

However I have no experie

3条回答
  •  后悔当初
    2021-01-25 03:20

    My solution is also similar to @jmsinger but you can specify the path to your files in the code itself and then calculate the mean like this :

    filename <- system("ls /dir/",intern=TRUE)
    
    for(i in 1:length(filename)){
    
    file <- read.table(filename[i],header=TRUE) ## if you have headers in your files ##
    mean <- mean(file$V4)
    
    write.table(mean,file=paste("/dir",paste("mean",filename[i],sep="."),sep="/")) 
    ##if you wish to write the means of all the files in seperate files rather than one.
    }
    

    hope this helps

提交回复
热议问题