Как читать в нескольких текстовых файлах в R с различным количеством столбцов - PullRequest
0 голосов
/ 20 апреля 2020

Я пытаюсь прочитать в нескольких текстовых файлах с разными столбцами в R. Я уже видел Как вы можете прочитать CSV-файл в R с различным количеством столбцов и пробовал с colClasses, а также col.names (оба с fill=T). Но это не работает. col.names дает мне invalid value for quotatines (что бы это ни было).

ep_dir <- "C:/Users/J/Desktop/e_prot_unicode"

чтение и объединение данных

* txt

# reading the data. empty list that gets filled up

ep_ldf<-list()

# creates a list of all the files in the directory with ending .txt

listtxt_ep<-list.files(path = ep_dir, pattern="*.txt", full.names = T) 

# loop for reading all the files in the list

for(m in 1:length(listtxt_ep)){
  ep_ldf[[m]]<-read.table(listtxt_ep[m],fill=T,header=T,sep = "\t",stringsAsFactors=F,fileEncoding = "UTF-16LE",dec = ",",colClasses = c("numeric", rep("character", 41)))
  ep <- bind_rows(ep_ldf,ep_ldf[[m]])
}

#another try because it is not working properly

f_ep = "C:/Users/J/Desktop/e_prot_unicode/22WS.U1"

#reading and merging the files, data.table is then called d_ep

d_ep = data.frame()
for(f_ep in listtxt_ep){
  tmp_ep <- read.delim(f_ep,row.names = NULL,sep = "\t",fileEncoding="UTF-16LE",fill = T,header = T,dec = ",",col.names = "V",seq_len(41)) %>% as.data.frame(stringsAsFactors = F)
  d_ep <- rbind.fill(d_ep, tmp_ep) 
}

Как читать в нескольких файлах с другим номером столбца в R?

1 Ответ

0 голосов
/ 20 апреля 2020

Не уверен, что я полностью следую за тем, что вы пытаетесь сделать, но функция dplyr bind_rows () позволяет вам комбинировать кадры данных с разными столбцами. Вы также можете передать список фреймов данных в bind_rows (), он объединит их все сразу, что упростит часть вашего кода.

...