error message meaning ??

#1
I have the following code to calculate the SPI drought index
Code:
mat <- matrix(c(2.26,
1.54,
2,
1.61,
2.75,
5.26,
4.78,
2.76,
0.829,
2.35,
3.4,
3,
8.28,
7.76,
2.14,
2.22,
0.939,
2.73,
2.18,
6.38,
6.66,
1.83,
2.41,
1.93,
2.83,
5.71,
4.4,
4.34,
2.28,
3.62,
1.35,
3.79,
2.58,
2.62,
5.02,
2.07,
1.95,
3.05,
4.49,
3.29,
6.44,
4.22,
1.96,
3.17,
0.973,
2.13,
2.76,
3.64,
3.87,
1.71,
1.19,
1.25,
2.21,
6.42,
4.21,
2.99,
2.84,
1.53,
3.32,
1.6,
4.49,
5.51,
4.45,
2.8,
2.6,
1.95,
2.34,
3.84,
4.25,
7.2
), ncol=10, nrow=7,
 dimnames=list(months=c("Jan","Feb" , "Mar","April","May","June","July"), 
c("2001", "2002","2003","2004","2005","2006","2007","2008","2009","2010")))

Library(spi)


write.table(mat,file="spi.txt",quote=FALSE,row.names=TRUE)

 spi(3,"spi.txt",2001,2010)

and I keep getting the following error message which I don't understand:

Error in data.frame(dates = dates, spi.plot[, 1:ncolumn]) :
arguments imply differing number of rows: 10, 9

Can any one help??

Thanks alot
 
#2
When the "spi.txt" file is written, you have 9 variable names in the first line followed by 10 values in the subsequent lines (it's not possible for write.table to include a name for the row.names column afaik). The SPI function expects a specific format with a name for each column. A workaround is to replace

Code:
write.table(mat,file="spi.txt",quote=FALSE,row.names=TRUE)
with:

Code:
write.table(cbind(Month=rownames(mat), mat), file="spi.txt", row.names=F, quote=F)
 
Last edited: