Hi,

I am trying to get the column sums from a dataset where i have 17 observations for each of the states. there are 48 states in the dataset. states are divided into regions. i was trying to run a loop and assign values for column sums to a new matrix. it doesn't work out. any help would be greatly appreciates. thanks.

the script:

data_matrix= read.csv("PRODUC.csv")

str(data_matrix)

data_matrix$Capital= data_matrix$P_CAP+ data_matrix$HWY+ data_matrix$WATER+ data_matrix$UTIL

gulf= subset(data_matrix, ST_ABB %in% c("AL", "FL", "LA", "MS"))

midwest= subset(data_matrix, ST_ABB %in% c("IL", "IN", "KY", "MI", "MN", "OH", "WI"))

mid_atlantic= subset(data_matrix, ST_ABB %in% c("DE", "MD", "NJ", "NY", "PA", "VA"))

MOUNTAIN= subset(data_matrix, ST_ABB %in% c("CO", "ID", "MT", "ND", "SD", "WY"))

NEW_ENGLAND= subset(data_matrix, ST_ABB %in% c("CT", "ME", "MA", "NH", "RI", "VT"))

SOUTH= subset(data_matrix, ST_ABB %in% c("GA", "NC", "SC", "TN", "WV", "RI"))

SOUTHWEST= subset(data_matrix, ST_ABB %in% c("AZ", "NV", "NM", "TX", "UT"))

CENTRAL= subset(data_matrix, ST_ABB %in% c("AK", "IA", "KS", "MO", "NE", "OK"))

WEST_COAST= subset(data_matrix, ST_ABB %in% c("CA", "OR", "WA"))

vector_1= rep.int(0, times = 4*9)

my_matrix_gulf=matrix(vector_1, nrow = 4, ncol = 9)

vectors_gulf_cumulative= for(i in seq(0, 68, by= 17)) for(j in c(1,2,3,4)) {

{

print(i)

my_matrix_gulf[j,1:9]= colSums(gulf[(i-16):i, 4:12])

print(my_matrix_gulf)

}

}

dataset can be downloaded from here https://drive.google.com/file/d/0B11woa7YyVb1dmlLV3hDVWUwRDA/view?usp=sharing