Skip to content
Prev 295610 / 398503 Next

Multiple cbind according to filename

Matthew Ouellette <mouellette89 <at> gmail.com> writes:
Hi Matthew,

You could try using substr() if the cbind is based on a common string in the
file name just makes sure that the strings in filenames is in the same order as
the files are in list.files:

a1 <- data.frame("col1" = seq(1,10, 1))
a2 <- data.frame("col2" = seq(11,20, 1))
b1 <- data.frame("col3" = seq(21,30, 1))
b2 <- data.frame("col4" = seq(31,40, 1))

filenames <- c("a1", "a2", "b1", "b2")

list.files <- list(a1, a2, b1, b2)
first.letter <- substr(filenames, 1,1)
unique.first.letter <- unique(first.letter)

l.files <- list()
for(i in 1:length(unique.first.letter)){
  l.files[[i]] = as.data.frame(list.files[first.letter == unique.first.letter[i]])
}


HTH,
Ken