2016-10-19 5 views
-1

Я хотел бы прочитать несколько текстовых файлов из моего каталога файлы располагаются в следующем форматеОшибка при чтении multple текстовых файлов из каталога в R

regional_vol_GM_atlas1.txt 
regional_vol_GM_atlas2.txt 
........ 
regional_vol_GM_atlas152.txt 

данных из файлов выглядит в следующем формате

667869 667869 
580083 580083 
316133 316133 
3631 3631 

Ниже приведен сценарий, который я написал

library(readr) 
library(stringr) 
library(data.table) 

array <- c() 
for (file in dir(/media/dev/Daten/Task1/subject1/t1)) # path to the directory where .txt files are located 
    { 

    row4 <- read.table(file=list.files(pattern ="regional_vol*.txt"), 
        header = FALSE, 
        row.names = NULL, 
        skip = 3, # Skip the 1st 3 rows 
        nrows = 1, # Read only the next row after skipping the 1st 3 rows 
        sep = "\t") # change the separator if it is not "\t" 
    array <- cbind(array, row4) 
} 

Я подвергаясь FOLLO крыло ошибка

Error in file(file, "rt") : invalid 'description' argument 

любезно предложить мне, где я был неправ в сценарии

+0

'MyFiles <- lapply (list.files (...), read.table (...))' лучший образец – arvi1000

+0

OP: Код эффективно проходит несколько файлов '(list.files()) 'to' read.table' –

+0

@RS значения хранятся в строке4, есть 900 файлов, поэтому я ожидаю 900 значений, но я вижу только 1 значение – DevanDevak

ответ

1

Это, кажется, работает хорошо для меня. Внесите необходимые изменения в комментариях к коду в случае файлов имеют заголовки: [Ответ Edited с учетом новой информации, размещенной OP]

# rm(list=ls()) #clean memory if you can afford to 

mydir<- "~/Desktop/a" #change as per your path 
# read full paths 
myfiles<- list.files(mydir,pattern = "regional_vol*",full.names=T) 
myfiles #check that files listed correctly 

# initialise the dataframe from first file 
# change header =T/F depending on presence of header 
# make sure sep is correct  

df<- read.csv(myfiles[1], header = F, skip = 0, nrows = 4, sep="")[-c(1:3),] 
#check that first line was read correctly 
df 
#read all the other files and update dataframe 
#we read 4 lines to read the header correctly, then remove 3 
ans<- lapply(myfiles[-1], function(x){ read.csv(x, header = F, skip = 0, nrows = 4, sep="")[-c(1:3),]  }) 
ans 


#update dataframe 
lapply(ans, function(x){df<<-rbind(df,x)} ) 

#this should be the required dataframe 
df 

Кроме того, если вы на Linux, гораздо простой метод был бы просто сделать ОС сделать это для вас

awk 'FNR == 4' regional_vol*.txt 
+0

Я хотел бы извлечь четвертую строку каждого текстового файла в массив, но когда я вижу рамку данных ans, я не вижу ничего импортируемого – DevanDevak

+0

Значения внутри myfiles пустые – DevanDevak

+0

, несмотря на то, что он задал правильный каталог в строке 1? –

0

Это должно сделать это для вас.

# set the working directory (where files are saved) 
setwd("C:/Users/your_path_here/Desktop/") 

file_names = list.files(getwd()) 
file_names = file_names[grepl(".TXT",file_names)] 

# print file_names vector 
file_names 

# read the WY.TXT file, just for testing 
# file = read.csv("C:/Users/your_path_here/Desktop/regional_vol_GM_atlas1.txt", header=F, stringsAsFactors=F) 

# see the data structure 
str(file) 

# run read.csv on all values of file_names 
files = lapply(file_names, read.csv, header=F, stringsAsFactors = F) 
files = do.call(rbind,files) 

# set column names 
names(files) = c("field1", "field2", "field3", "field4", "field5") 
str(files) 


write.table(files, "C:/Users/your_path_here/Desktop/mydata.txt", sep="\t") 
write.csv(files,"C:/Users/your_path_here/Desktop/mydata.csv") 
Смежные вопросы