Записывать список матриц в csv? - PullRequest
0 голосов
/ 01 июня 2018

Как я могу записать список матриц в CSV-файл в R?

Я пытался

fff = tr[[3]]  
 MATweight = ldply(fff, function(t) t$toDataFrame())

, но с этой ошибкой

Error in t$toDataFrame : $ operator is invalid for atomic vectors

Яне уверен, что это было правильно, любая идея, пожалуйста?

У меня есть этот список матриц

    > str(fff)
    List of 10
     $ : num [1:1000, 1:50] 1 1 1 1 2 2 1 2 2 1 ...
....
     $ : num [1:1000, 1:50] 1 1 1 1 2 2 1 2 2 1 ..

.

Когда я попробовал предложенный ответГ. Я получил:

 > write.csv(map_dfr(fff, as.data.frame, .id = "matrix"),"testt.csv",  row.names = FALSE)
    > tgtg = read.csv("/Users/amani/testt.csv")
    > str(tgtg)
    'data.frame':   10000 obs. of  51 variables:
     $ matrix: int  1 1 1 1 1 1 1 1 1 1 ...
     $ V1    : int  1 1 1 1 2 2 1 2 2 1 ...
     $ V2    : int  2 2 1 2 1 1 2 1 1 2 ...
     $ V3    : int  1 1 1 1 1 2 1 1 2 1 ...
....
     $ V50   : int  2 2 1 1 1 1 1 1 2 2 ...

Ответы [ 2 ]

0 голосов
/ 03 июня 2018

Почему бы не сделать это так:

# sample data like your `fff` list of matrices
fff <- lapply(1:10, function(i) 
  matrix(data = sample(1:2, 510000, replace = TRUE), nrow = 10000, ncol = 51))


# we give each matrix in the list a unique name
mat_names <- as.character(1:length(fff))

# converting to dataframe with the list index preserved
fffdf <- lapply(1:length(fff), function(i)
  cbind(mat_names = mat_names[i], as.data.frame(fff[[i]])))
fffdf <- do.call(rbind, fffdf)

# writing it to a file
t <- tempfile(fileext = ".csv")
write.csv(fffdf, t, row.names = FALSE)

# testing the reassembly into the same list of matrices that you started
tcsv <- read.csv(t)
tgtg <- lapply(mat_names, function(i) {
  df <- subset(tcsv, mat_names == i, select = -mat_names)
  mat <- as.matrix(df)
  dimnames(mat) <- NULL
  return(mat)
})

Проверка результата: tgtg совпадает с fff, с которого вы начали?

> identical(fff, tgtg)
[1] TRUE

> str(tgtg)
List of 10
 $ : int [1:10000, 1:51] 1 2 1 2 1 1 2 2 1 1 ...
 $ : int [1:10000, 1:51] 2 2 2 2 2 2 1 1 1 1 ...
 $ : int [1:10000, 1:51] 1 2 1 1 2 2 1 1 2 1 ...
 $ : int [1:10000, 1:51] 1 1 2 2 1 1 1 2 1 1 ...
 $ : int [1:10000, 1:51] 1 1 2 1 2 2 1 1 1 2 ...
 $ : int [1:10000, 1:51] 2 1 2 2 1 1 1 2 1 2 ...
 $ : int [1:10000, 1:51] 2 1 2 1 2 1 1 1 2 1 ...
 $ : int [1:10000, 1:51] 2 2 1 1 2 2 1 2 1 1 ...
 $ : int [1:10000, 1:51] 1 2 1 1 1 1 1 2 2 1 ...
 $ : int [1:10000, 1:51] 1 1 1 2 2 2 2 2 2 2 ...
0 голосов
/ 01 июня 2018

Используя показанные тестовые данные L, попробуйте map_dfr из purrr:

library(purrr)

L <- list(as.matrix(BOD), as.matrix(10*BOD)) # test data

write.csv(map_dfr(L, as.data.frame, .id = "matrix"), stdout(), row.names = FALSE)

, давая:

"matrix","Time","demand"
"1",1,8.3
"1",2,10.3
"1",3,19
"1",4,16
"1",5,15.6
"1",7,19.8
"2",10,83
"2",20,103
"2",30,190
"2",40,160
"2",50,156
"2",70,198
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...