Melinda Hodkiewicz
Melinda Hodkiewicz

Reputation: 1

reading data from a number text files in R to a single text file

I have 198 txt files in a single directory. The filenames are stored in a list called filelist. Each file has 7 columns and a different number of rows (100-200). One file appears to have 8 columns. I used the following code

dat<-data.frame()
for (i in 1:n)
{
    dat<-rbind.fill(dat,read.table(filelist[i],sep=",",header=TRUE))
}

I get the error

"Error in read.table(filelist[i], sep = ",", header = TRUE) : duplicate 'row.names' are not allowed".

Some posts suggest I should add rownames=NULL but I tried this and get the error message

"Error in read.table(filelist[i], sep = ",", header = TRUE, rownames = NULL) : unused argument (rownames = NULL)"

All guidance would be much appreciated.

Upvotes: 0

Views: 1472

Answers (1)

Md. Sahidul Islam
Md. Sahidul Islam

Reputation: 583

Create three *.txt dataset (each have 100 rows and 2 columns) and save it to working directory according to filelist....

setwd("C:\\Users\\Sahidul.Islam\\Desktop\\R")
#[1] "C:/Users/Sahidul.Islam/Desktop/R"
getwd()

data<-matrix(rnorm(200),100,2)
colnames(data)<-c("var1","var2")

filelist<-c("dat1.txt","dat2.txt","dat3.txt")

for (i in 1:length(filelist)){
write.table(data,filelist[i],row.names=F)
    }

Call three *.txt datasets (created above) and bind the rows to make a compact data...

dat<-data.frame(read.table("dat1.txt",header=T))

for (i in 2:length(filelist))
    {
    dat<-rbind(dat,read.table(filelist[i],header=TRUE))
    }

Check the dimension of final dataset.

dim(dat)
#[1] 300   2

Hope this will work.

Upvotes: 1

Related Questions