-2

I have four data frames , each data frame has the same number of records and same columns: it looks like this:

    CURRENT  30DPD   60DPD  90DPD
1   0.56     56.67    67.6  57.67
2   0.24     56.78    7.6   24.67
3   0.32     56.11    66    34.67
4..........

Now from each df I am taking the first row and creating a list, then the second row from each table, the third row from each table and so on. now trying to convert a large list to matrixes and here is the code I am using:

for ( i in 1:3542) {  
   vec1 <- One[i,]
   vec2 <- two[i,]
   vec3 <- Three[i,]
   vec4 <- Four[i,]
   tab[[i]] <- c(vec1,vec2,vec3,vec4)
   final[[i]] <-matrix(unlist(tab[[i]]),nr=4,nc=4)
}

In the list, I have 3542 elements and I am getting an error when I use this code.

Error in final[[i]] <- matrix(unlist(tab[[i]]), nr = 4, nc = 4) :
more elements supplied than there are to replace

But if I don't use an index then it works fine but for 3000 elements it would be very time-consuming. Any idea how can I create 3542 seperate matrixes using the list?

Thank you

N8888
  • 670
  • 2
  • 14
  • 20
SUBHRA SANKHA
  • 118
  • 1
  • 2
  • 11

2 Answers2

0

First make sure you have the same number of elements in your list :

length(unique(lengths(your_list)) # should be one

Then take a look at the classes you have in there, to see if your really can/want to coerce them into a matrix :

unique(sapply(your_list, function(x) paste(class(x),collapse= " ")))

Then I believe the easiest would be to convert first to data.frame then to matrix :

mat <- as.matrix(as.data.frame(your_list))
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • I have updated the question as the heads were not clear. The solution you provided is not what I am looking for but thank you so much. – SUBHRA SANKHA Jul 05 '18 at 11:00
0

You can do:

s = list(dat1,dat2,dat3,dat4) 
  do.call(Map,c(rbind,lapply(s,function(x)data.frame(t(x)))))
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • I was hoping for individual matrices, thank you though – SUBHRA SANKHA Jul 05 '18 at 11:17
  • @SUBHRASANKHA what do you mean by individual matrices?? – Onyambu Jul 05 '18 at 11:18
  • so I have 3542 rows in each data frame and earlier I was taking the first row from each table and creating a list and then creating a matrix , then second row from each table > list > matrix , then third row from each table ans so on. – SUBHRA SANKHA Jul 05 '18 at 11:23
  • @SUBHRASANKHA That is exactly what this code is doing. If you want to test the code. Just store the first 3 rows form each data as s ie `s=list(dat1[1:3,],dat2[1:3,],dat3[1:3,],dat4[1:3,])` then run the next line and see whether the output is not what you want then let me know – Onyambu Jul 05 '18 at 11:26
  • yes this works like a charm :) Thank you so much and sorry for the confusion :P – SUBHRA SANKHA Jul 05 '18 at 11:43
  • @SUBHRASANKHA you can also do: `s=rbind(dat1,dat2,dat3,dat4);tapply(unlist(s),rep(row(dat1),4),matrix,ncol=4)` then you can decide which works faster for you – Onyambu Jul 05 '18 at 11:49