来源:http://bbs.pinggu.org/thread-1219536-1-1.html
################ 生成数据
bh=1:100
jxh=rep(1:17,c(4,5,6,7,8,4,5,6,7,8,4,5,5,5,6,7,8))relation=c(0,5,6,10,0,5,6,10,11,0,5,6,10,11,12,0,5,6,10,11,12,13,0,5,6,10,11,12,13,14, 0,5,6,10,0,5,6,10,11,0,5,6,10,11,12,0,5,6,10,11,12,13,0,5,6,10,11,12,13,14, 0,5,6,10,0,5,6,10,11,0,5,6,10,11,0,5,6,10,11,0,5,6,10,11,12,0,5,6,10,11,12,13, 0,5,6,10,11,12,13,14)nn=length(jxh) ##nn=100
jx=unique(jxh) ##jx=17datajx=data.frame(jxh,relation,bh)data1=data.frame(datajx[relation==5,])
data2=data.frame(datajx[relation==6,])data1=data1[,-2]data2=data2[,-2]colnames(data1)[2]="did"colnames(data2)[2]="mid"data3=merge(datajx,data1,by.x="jxh",by.y="jxh")data4=merge(data3,data2,by.x="jxh",by.y="jxh") ###data4=merge(data3,data2,by="jxh")################ 不用for循环,实现赋值
data5 <- data4
data5[(relation == 5) | (relation == 6), 4:5] <- 0