Explore Courses Blog Tutorials Interview Questions
0 votes
in R Programming by (50.2k points)

Basically I am having 20 csv. files and want to convert one of the columns (logF0) in each dataframe into a 20*16 matrix. The result should be 20 matrixs of dimention 20*16 and I hope the names of the matrix could be the same as the csv. files. I thought I could use a loop to convert the dataframe separately to matrix at first:

files_list<-list.files("my_path", full.names = TRUE, pattern = "*.csv") 

for(i in 1: length(files_list))



  file[i]<-rbind(dat, read.csv(files_list[i]))

  mat[i]<-rbind(mat, matrix(file[i]$logF0, nrow=20, byrow = F))}

The above code doesn't work. Kindly provide a solution for that...

1 Answer

0 votes
by (108k points)

See, in R programming, what you can do is use the lapply() and in that pass your matrix dimensions: 

all_data <- lapply(files_list, function(file) {

   df <- read.csv(file)

   matrix(df$$logF0, nrow = 20)


If you want to give this list names, you could do :

names(all_data) <- sub('\\.csv', '', basename(file_list))

Browse Categories