0

I need to create several matrices based on two criterion: ideo and time. Here is a part of my code which does not work. I see the problem in using list object to store the numbers from new subsets but I don't know the way to list i and j simultaneously in the list. Should I make list(list())? What other ways to code this problem?

ideo.list<-list(f1,f2,f3,f4,f5,f6,f7,f8,f9)
time.list<-list(t1,t2,t3,t4)
dattime.list<-list()
for (j in 1:length(time.list)){
  for (i in 1:length(ideo.list)){
    dat.sub<-subset(dat,iyear %in% time.list[[j]] & Ideo %in% ideo.list[[i]])
    dattime.list[[i*j]]<-apply(dat.sub[,5:13],2,sum)
}}
nn<-matrix(unlist(dattime.list), byrow=TRUE, ncol=9,nrow=length(dattime.list) )

The head of input data is below:

iyear                                                     Ideo Armed.Assault Assassination
1  1982 Separatist / New Regime Nationalist / Ethnic Nationalist             0             0
2  1994 Separatist / New Regime Nationalist / Ethnic Nationalist             0             0
3  1995                   Left Wing Terrorist Groups (Anarchist)             0             0
4  2010                                  Racist Terrorist Groups             1             0
5  2013                   Left Wing Terrorist Groups (Anarchist)             0             0
6  2014                       Cell Strategy and Terrorist Groups             0             0
  Bombing.Explosion Facility.Infrastructure.Attack Hijacking Hostage.Taking..Barricade.Incident.
1                 1                              0         0                                   0
2                 0                              1         0                                   0
3                 0                              1         0                                   0
4                 0                              0         0                                   0
5                 0                              1         0                                   0
6                 0                              1         0                                   0
  Hostage.Taking..Kidnapping. Unarmed.Assault Unknown
1                           0               0       0
2                           0               0       0
3                           0               0       0
4                           0               0       0
5                           0               0       0
6                           0               0       0

Thank you for help!

  • Please use the answer here to create a reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Divi Jan 08 '18 at 18:49
  • Is *dat* really a matrix or dataframe? And do explain *which does not work*? Errors? Undesired results? – Parfait Jan 08 '18 at 18:58
  • @Parfait dat is the data frame. I cannot receive the result I'm looking for. I need to generate 36 matrices (by ideo and time, 9*4). It should work similar to pivot table in excel. – Lyudmyla Starostyuk Jan 08 '18 at 19:52
  • What result do you receive? And can you `dput` a few rows of data? – Parfait Jan 08 '18 at 19:58
  • @Parfait I'm getting warning message: In matrix(unlist(dattime.list), byrow = TRUE, ncol = 9, nrow = length(dattime.list)) : data length [198] is not a sub-multiple or multiple of the number of rows [36] and the results is like this: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 326 1048 1036 166 6 14 58 0 18 [2,] 323 750 1444 450 8 6 38 1 8 [3,] 80 11 115 108 0 1 6 5 8 [4,] 0 0 11 31 0 1 1 0 0 [5,] 39 53 32 12 0 14 13 1 66 – Lyudmyla Starostyuk Jan 08 '18 at 20:01
  • This output means nothing to us without input data. What are the values of *ideo.list* and *time.list*? Please update your post (see *edit* link) rather than long comments. – Parfait Jan 08 '18 at 20:16

0 Answers0