Home > OS >  Generating multiple csv from a list
Generating multiple csv from a list

Time:05-10

Using the markovchain library, I managed to generate 144 transitions probability matrixes (eg. matrixList), but how can I save all (143) as a .csv file?

library(tidyverse)
library(markovchain)

mcListFist<-markovchainListFit(data=df[, 1:144], name="df")

matrixList<-list()
for (i in 1:dim(mcListFist$estimate)) {
  myMatr<-  mcListFist$estimate[[i]]@transitionMatrix
  matrixList[[i]]<-myMatr
}

matrixList

Output of transition probability matrix 84-85

............

[[84]]
  0
0 1

[[85]]
  0
0 1

Sample data:

df<-structure(list(`04:00` = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 
11), `04:10` = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 11), `04:20` = c(11, 
11, 11, 11, 11, 11, 11, 11, 11, 11), `04:30` = c(11, 11, 11, 
11, 11, 11, 11, 11, 11, 11), `04:40` = c(11, 11, 11, 11, 11, 
11, 11, 11, 11, 11), `04:50` = c(11, 11, 11, 11, 11, 11, 11, 
11, 11, 11), `05:00` = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 
11), `05:10` = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 11), `05:20` = c(11, 
11, 11, 11, 11, 11, 11, 11, 11, 11), `05:30` = c(11, 11, 11, 
11, 11, 11, 11, 11, 11, 11), `05:40` = c(11, 11, 11, 11, 11, 
11, 11, 11, 11, 11), `05:50` = c(11, 11, 11, 11, 11, 11, 11, 
11, 11, 11), `06:00` = c(11, 0, 11, 11, 11, 11, 11, 0, 0, 11), 
    `06:10` = c(11, 0, 11, 11, 11, 11, 11, 0, 0, 11), `06:20` = c(11, 
    0, 11, 11, 11, 11, 11, 0, 0, 11), `06:30` = c(11, 0, 11, 
    11, 11, 11, 11, 0, 0, 0), `06:40` = c(11, 0, 11, 11, 11, 
    11, 11, 0, 0, 0), `06:50` = c(11, 0, 11, 11, 11, 11, 11, 
    0, 0, 0), `07:00` = c(11, 0, 11, 0, 11, 11, 11, 0, 0, 0), 
    `07:10` = c(11, 0, 11, 0, 11, 11, 11, 0, 0, 0), `07:20` = c(11, 
    0, 11, 0, 11, 11, 11, 0, 0, 0), `07:30` = c(11, 0, 11, 0, 
    11, 11, 11, 0, 0, 0), `07:40` = c(0, 0, 11, 0, 11, 11, 11, 
    0, 0, 0), `07:50` = c(0, 0, 11, 0, 11, 11, 11, 0, 0, 0), 
    `08:00` = c(0, 0, 0, 0, 0, 11, 11, 0, 0, 0), `08:10` = c(0, 
    0, 0, 0, 0, 11, 11, 0, 0, 0), `08:20` = c(0, 0, 0, 0, 0, 
    11, 11, 0, 0, 0), `08:30` = c(0, 0, 0, 0, 0, 11, 11, 0, 0, 
    0), `08:40` = c(0, 0, 0, 0, 0, 11, 11, 0, 0, 0), `08:50` = c(0, 
    0, 0, 0, 0, 11, 11, 0, 0, 0), `09:00` = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), `09:10` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    `09:20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `09:30` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `09:40` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `09:50` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `10:00` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `10:10` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `10:20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `10:30` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `10:40` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `10:50` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `11:00` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `11:10` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `11:20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `11:30` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `11:40` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `11:50` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `12:00` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `12:10` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `12:20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `12:30` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `12:40` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `12:50` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `13:00` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `13:10` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `13:20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `13:30` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `13:40` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `13:50` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `14:00` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `14:10` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `14:20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `14:30` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `14:40` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `14:50` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `15:00` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `15:10` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `15:20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `15:30` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `15:40` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `15:50` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `16:00` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `16:10` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `16:20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `16:30` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `16:40` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `16:50` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `17:00` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `17:10` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `17:20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `17:30` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `17:40` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `17:50` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `18:00` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `18:10` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `18:20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `18:30` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `18:40` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `18:50` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `19:00` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `19:10` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `19:20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `19:30` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `19:40` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `19:50` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `20:00` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `20:10` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `20:20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `20:30` = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), `20:40` = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), `20:50` = c(0, 11, 11, 0, 0, 0, 0, 0, 0, 0), 
    `21:00` = c(0, 11, 11, 0, 0, 0, 0, 0, 0, 11), `21:10` = c(0, 
    11, 11, 0, 0, 0, 0, 0, 0, 11), `21:20` = c(0, 11, 11, 0, 
    0, 0, 0, 0, 0, 11), `21:30` = c(0, 11, 11, 0, 0, 0, 0, 0, 
    0, 11), `21:40` = c(0, 11, 11, 0, 0, 0, 0, 0, 0, 11), `21:50` = c(11, 
    11, 11, 0, 0, 0, 0, 0, 0, 11), `22:00` = c(11, 11, 11, 0, 
    0, 0, 0, 0, 0, 11), `22:10` = c(11, 11, 11, 0, 0, 0, 11, 
    0, 0, 11), `22:20` = c(11, 11, 11, 0, 0, 0, 11, 0, 0, 11), 
    `22:30` = c(11, 11, 11, 0, 0, 11, 11, 11, 11, 11), `22:40` = c(11, 
    11, 11, 0, 0, 11, 11, 11, 11, 11), `22:50` = c(11, 11, 11, 
    0, 0, 11, 11, 11, 11, 11), `23:00` = c(11, 11, 11, 0, 0, 
    11, 11, 11, 11, 11), `23:10` = c(11, 11, 11, 0, 0, 11, 11, 
    11, 11, 11), `23:20` = c(11, 11, 11, 0, 0, 11, 11, 11, 11, 
    11), `23:30` = c(11, 11, 11, 0, 0, 11, 11, 11, 11, 11), `23:40` = c(11, 
    11, 11, 0, 0, 11, 11, 11, 11, 11), `23:50` = c(11, 11, 11, 
    0, 11, 11, 11, 11, 11, 11), `00:00` = c(11, 11, 11, 0, 11, 
    11, 11, 11, 11, 11), `00:10` = c(11, 11, 11, 0, 11, 11, 11, 
    11, 11, 11), `00:20` = c(11, 11, 11, 0, 11, 11, 11, 11, 11, 
    11), `00:30` = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 11), 
    `00:40` = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 11), `00:50` = c(11, 
    11, 11, 11, 11, 11, 11, 11, 11, 11), `01:00` = c(11, 11, 
    11, 11, 11, 11, 11, 11, 11, 11), `01:10` = c(11, 11, 11, 
    11, 11, 11, 11, 11, 11, 11), `01:20` = c(11, 11, 11, 11, 
    11, 11, 11, 11, 11, 11), `01:30` = c(11, 11, 11, 11, 11, 
    11, 11, 11, 11, 11), `01:40` = c(11, 11, 11, 11, 11, 11, 
    11, 11, 11, 11), `01:50` = c(11, 11, 11, 11, 11, 11, 11, 
    11, 11, 11), `02:00` = c(11, 11, 11, 11, 11, 11, 11, 11, 
    11, 11), `02:10` = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 
    11), `02:20` = c(11, 11, 11, 11, 11, 11, 0, 11, 11, 11), 
    `02:30` = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 11), `02:40` = c(11, 
    11, 11, 11, 11, 11, 11, 11, 11, 11), `02:50` = c(11, 11, 
    11, 11, 11, 11, 11, 11, 11, 11), `03:00` = c(11, 11, 11, 
    11, 11, 11, 11, 11, 11, 11), `03:10` = c(11, 11, 11, 11, 
    11, 11, 11, 11, 11, 11), `03:20` = c(11, 11, 11, 11, 11, 
    11, 11, 11, 11, 11), `03:30` = c(11, 11, 11, 11, 11, 11, 
    11, 11, 11, 11), `03:40` = c(11, 11, 11, 11, 11, 11, 11, 
    11, 11, 11), `03:50` = c(11, 11, 11, 11, 11, 11, 11, 11, 
    11, 11)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
"data.frame"))

CodePudding user response:

Apply the writing function to every element of the list:

mapply(function(data, name) {
  data <- as.data.frame(data)
  write.csv(data, paste0(name, ".csv"))
}, matrixList, 1:length(matrixList))
  • Related