Looping through files in R

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

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 experience building loops involving R, only with bash. How would I convert this into a loop that did this for every file in a folder and saved the output into one file with the file name and mean value as the 2 columns for each row? eg:

x01(or file1 if that is simpler) 23.4 x02 25.4 x03 10.4 

etc

(Don't mind if the solution is bash and R or exclusively R) Many thanks for your help!

Current error from one of the solutions using bash and R:

Error in `[.data.frame`(read.table("PercentWindowConservedRanked_Lowest_cleanfor1000genomes_1000regions_x013",  :    undefined columns selected Calls: mean -> [ -> [.data.frame Execution halted 

回答1:

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



回答2:

This is similar to what @jmsigner has done, but with minor changes. For instance, writing to a file is done at the end. The code has not been tested.

out <- lapply(list.files(), FUN = function(x) {     m <- mean(read.table(x, header = TRUE)$V4)     return(m)   }) result <- do.call("cbind", out) #merge a list column-wise # before writing, you can make column names pretty with colnames() # e.g. colnames(result) <- c("x01", "x02") write.table(result, file = "means.txt") 


回答3:

Assuming the columns are always named the same, you could do the following in R:

out.file <- 'means.txt' for (i in list.files()) {     tmp.file <- read.table(i, header=TRUE)  # Not sure if you have headers or not     tmp.mean <- mean(tmp.file1$V4)     write(paste0(i, "," tmp.mean), out.file, append=TRUE) } 

Or the same thing with more bash:

for i in $(ls *) do   mean=$(Rscript -e "mean(read.table('$i', header=T)[, 'V4'])")   echo $i,$mean >> means.txt done 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!