1

I have several txt files, and their names are 1.txt, 2.txt,3.txt and … 100.txt

I want to read these files in R language in loop. My code is:

For(I in 1:100){

    Datai<-read.table(“H://”+’i’+”.txt”)

}

But when I run I get this error:

non-numeric argument to binary operator

How can I solve this problem?

Belphegor
  • 4,456
  • 11
  • 34
  • 59
sahar
  • 145
  • 2
  • 3
  • 8

2 Answers2

2
For(I in 1:100){

Datai<-read.table(paste(paste("H://",i,".txt",sep="")))

}

As far as I know there is no string concatenate operator in R.

Here is a question how to construct one.

Community
  • 1
  • 1
Trudbert
  • 3,178
  • 14
  • 14
1

In your loop the object Datai is replaced by the new object in each run. You should store the data frames returned by read.table in a list instead.

Data <- vector("list", length = 100) # initialize the list
For(I in 1:100){    
    Data[[I]] <- read.table(paste("H://", I, ".txt", sep = ""))    
}

The same could be achieved with lapply:

Data <- lapply(1:100, function(I) read.table(paste("H://", I, ".txt", sep = "")))
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168