0

My question is very simple I searched it and thought that I found the solution but I get an error. Here's my problem:

I have some files like this:

Mor_ldnew1.txt
Mor_ldnew2.txt
Mor_ldnew3.txt
.
.
.

And I want to store each of this files in different objects to then merge them but I got an error with my loop:

    for (i in 1:3){

read.table(paste("Mor_ld_new",i,".txt", sep="")) -> mor[i]      
}

This is the error:

Error: unexpected '}' in "        }"

Thanks in advance.

JM88
  • 457
  • 3
  • 6
  • 11
  • What kind of syntax is `mor([i])`? Also, use `lapply`. There are many answers on SO, which show how to import files from a directory using `lapply`, e.g. [here](http://stackoverflow.com/a/9565095/1412059). – Roland Feb 05 '14 at 16:22

2 Answers2

2

You need to remove the parentheses around the [i].

A more idiomatic way to write it would to use lapply.

Christopher Louden
  • 7,540
  • 2
  • 26
  • 29
0

Using the loop:

for (i in 1:3){
assign(paste0("mor", i), read.table(paste("Mor_ld_new",i,".txt", sep="")))
}
Carlos Cinelli
  • 11,354
  • 9
  • 43
  • 66