So I have 80 files that come in the file name format:
P.A3588.ACO.CCLF0.ROW1.ROW30000
P.A3588.ACO.CCLF1.ROW1.ROW30000
P.A3588.ACO.CCLF0.ROW30001.ROW60000
There are 80 fixed width text files: 8 parts for each of the 10 CCLF numbers (CCLF0,CCLF1,...,CCLF9). I want to be able to group by CCLF number, apply the column width vector, and bind the rows of the CCLF parts.
Below is what I've tried so far. It doesn't work, but gives an idea what I'm attempting.
filenames <- list.files(dataPath)
names <- substr(filenames,13,17)
CCLF1_width <- c(13,6,11,2,10,10,1,1,7,7,2,17,1,2,2,4,1,10,10,10,10,10,2,10,10,10,11,2,2,1,1,1)
CCLF2_width <- c(13,10,11,2,10,10,4,10,5,11,6,10,10,24,17,2,2,2,2,2)
CCLF3_width <- c(13,11,2,2,7,10,11,6,10,10,1)
CCLF4_width <- c(13,11,2,1,2,7,11,6,10,10,7,1)
CCLF5_width <- c(13,10,11,2,10,10,3,2,2,1,2,10,10,5,15,1,7,10,10,2,2,2,10,10,40,11,17,24,2,2,2,2,2,2,7,7,7,7,7,7,7,7,1)
CCLF6_width <- c(13,10,11,2,10,10,1,2,10,10,5,15,1,10,10,2,2,2,10,10,40,11,17,2)
CCLF7_width <- c(13,11,11,2,10,2,20,1,1,24,9,2,20,13,2,10,10,12,9)
CCLF8_width <- c(11,2,3,5,10,1,1,3,2,2,10,10,10,30,15,40,1,1)
CCLF9_width <- c(11,11,10,10,12)
CCLF0_width <- c(11,11)
for (i in length(filenames)){
assign(paste0(substr(filenames,13,17)), read_fwf(grepl("CCLF1",filenames),paste0(i,"_width")))
}
You can use list.files
with argument recursive = True
and full.names = TRUE
to get file path of all 80 txt files, And then use lapply
or for
with read.table
to read all the files in one list
which has 80 element, the use do.call(rbind,your_list)
to combine the list to one data frame.
filename<-list.files(folder_path,recursive = T,pattern = ".txt",full.names = T)
all_file<-lapply(filename,read.table)
df<-do.call(rbind,all_file)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.