Loading multiple files into matrix using R

后端 未结 2 396
栀梦
栀梦 2021-01-06 10:25

I am new to the programming world and need help with loading a file to R and creating a matrix with it. I can import individual files and create and individual matrix out o

相关标签:
2条回答
  • 2021-01-06 10:29

    It's not exactly clear what structure you want. You can choose between a 2100x100 matrix or a 2100x100 dataframe or a 100x 100x 21 array or a list with 21 entries each of which was 100 x 100. (In R an array is the term one would use for a regular 3 dimensional structure with columns all of that same type. (and then of course there is agstudy's suggestion that you use a data.table.)

    In a sense agstudy's code already gives you the 21 item list of dataframes each of dimension: 100x100:

    temp = list.files(pattern="*.csv")
    named.list <- lapply(temp, read.csv)
    

    To get 100 x 100 x 21 array continue with this:

    require(abind)
    arr <- abind(named.list)
    

    To get the 2100 x 100 dataframe, continue instead with:

    longdf <- do.call(rbind, named.list)
    

    To get the 2100 x 100 matrix continue from the last line with:

    longmtx <- data.matrix(longdf)
    
    0 讨论(0)
  • 2021-01-06 10:33
    1. I would use list.files to list my files by pattern.
    2. lapply to loop through the list of files and create a list data.frame with read.csv
    3. rbindlist to bind all in a big matrix.

      temp = list.files(pattern="*.csv")
      named.list <- lapply(temp, read.csv)
      library(data.table)
      files.matrix <-rbindlist(named.list)
      
    0 讨论(0)
提交回复
热议问题