library(openxlsx) sample1<-read.xlsx("http://kanggc.iptime.org/book/data/cpi-e.xlsx") weight<-read.xlsx("http://kanggc.iptime.org/book/data/cpi-weight-e.xlsx") all<-ts(sample1$all, start=2015, end=2017, frequency=1) fo<-ts(sample1$food, start=2015, end=2017, frequency=1) al<-ts(sample1$alcohol, start=2015, end=2017, frequency=1) cl<-ts(sample1$clothing, start=2015, end=2017, frequency=1) ho<-ts(sample1$housing, start=2015, end=2017, frequency=1) hou<-ts(sample1$household, start=2015, end=2017, frequency=1) he<-ts(sample1$health, start=2015, end=2017, frequency=1) tr<-ts(sample1$transport, start=2015, end=2017, frequency=1) co<-ts(sample1$communication, start=2015, end=2017, frequency=1) re<-ts(sample1$recreation, start=2015, end=2017, frequency=1) ed<-ts(sample1$education, start=2015, end=2017, frequency=1) res<-ts(sample1$restaurant, start=2015, end=2017, frequency=1) ot<-ts(sample1$others, start=2015, end=2017, frequency=1) w1<-weight[1,2] w2<-weight[2,2] w3<-weight[3,2] w4<-weight[4,2] w5<-weight[5,2] w6<-weight[6,2] w7<-weight[7,2] w8<-weight[8,2] w9<-weight[9,2] w10<-weight[10,2] w11<-weight[11,2] w12<-weight[12,2] w13<-weight[13,2] z0<-as.matrix(cbind(all,fo,al,cl,ho,hou,he,tr,co,re,ed,res,ot)) z0 z1<-matrix(data=NA, nrow=2, ncol=12, byrow=T) z1 w<-as.matrix(cbind(w1,w2,w3,w4,w5,w6,w7,w8,w9,w10,w11,w12,w13)) for(i in 1:2) { for(j in 1:12) { z1[i,j]<-(((z0[i+1,j+1]-z0[i,j+1])*w[j+1])/((z0[i+1,1]-z0[i,1])*w1))*100 } } (z1<-round(z1, digits=4)) (rowSums(z1)) z2<-matrix(data=NA, nrow=2, ncol=12, byrow=T) for(i in 1:2) { for(j in 1:12) { z2[i,j]<-(z0[i+1,1]-z0[i,1])/(z0[i,1])*z1[i,j] } } (z2<-round(z2, digits=4)) (rowSums(z2))