使用for循环创建多个子集

时间:2018-01-08 18:20:25

标签: r

我需要根据两个标准创建几个矩阵:ideo和time。 这是我的代码的一部分,它不起作用。我看到使用列表对象存储新子集中的数字时遇到的问题,但我不知道在列表中同时列出ij的方法。我应该list(list())吗?还有什么其他方法来编码这个问题?

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) )

输入数据的头部如下:

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

谢谢你的帮助!

0 个答案:

没有答案