0

I have four(say) .Rda files where each file consists a vector of observations.

I like to compute simple mean vector whose elements computed from different file

mean1=array(0,dim=4)
for (i in 1:4){
mean1[i]= mean(readRDS(file = "i-th file "))
}

How to do this? I am using simple mean function to illustrate the my point but eventually I will do to more than one things with in a loop after using i-th file.

I read different materials like this but could not apply it here.

Janak
  • 653
  • 7
  • 25
  • What do your filenames look like? – sebastian-c Nov 23 '16 at 12:47
  • @sebastian-c Here is a file name I am working on "data/2001.Rda" – Janak Nov 23 '16 at 12:48
  • 1
    Maybe `lapply(list.files(),function(i)mean(readRDS(i)))` ? – zx8754 Nov 23 '16 at 12:51
  • 1
    `files <- list.files("data", pattern = "\\.Rda$")` and in `ReadRDS` use `file = files[i]` – sebastian-c Nov 23 '16 at 12:51
  • @sebastian-c. After following the suggested line (works fine) but getting error while reading in the file. That is readRDS(file=files[i]) is giving error: cannot open the connection – Janak Nov 23 '16 at 13:22
  • What is the value of `i` when the error occurs? What is the value of `files[i]` then? – sebastian-c Nov 23 '16 at 13:25
  • @sebastian-c For i=1 , files[i]= "2001.Rda". I am not sure if "readRDS(file=files[i])" is correct or not for reading files. – Janak Nov 23 '16 at 13:27
  • 1
    Try adding `full.names = TRUE` to your `list.files` call – sebastian-c Nov 23 '16 at 13:49
  • @sebastian-c. Its is working now. Thank you very much. Your suggestion greatly helping me to solve a long standing problem in my code. I got the answer of my question which I found nowhere. I will be happy to accept your suggestion as answer to my question. – Janak Nov 23 '16 at 14:20
  • @Janak The question has been closed, I cannot answer it. – sebastian-c Nov 23 '16 at 14:47
  • @sebastian-c. Though it is now closed, your answer is helpful to my work. Thank you for all your helps. – Janak Nov 24 '16 at 07:05

0 Answers0