I am trying to save multiple raster pixel values into a single data frame and store as csv format. I extracted my data for single file and store as CSV. However, I could not store other raster values in same csv file. It is replaced with previous values. I know its little error at but I am stuck in it.
The code is given below:
> rastlist <- mixedsort(list.files(path = "/shared/MODIS/2003-2008/", pattern='.tif',
> all.files=TRUE, full.names=FALSE,recursive = TRUE))
> shp= readOGR("Gilgit_DEM_57.shp")
> c = as.data.frame(matrix(NA,nrow = 1,ncol = 1))
> i = 1
> for (i in 1:length(rastlist)){
> raster1 = raster(rastlist[i])
> r2_extract = extract(raster1,shp,df = T, na.rm = T)
> b1 =as.data.frame(table(r2_extract))
> colnames(b1) = c("ID","MOD10A","Freq")
> b3_200 = as.data.frame(b1[which(b1$MOD10A == 200),])
> b3_198 = as.data.frame(b1[which(b1$MOD10A == 198),])
> b3_199 = as.data.frame(b1[which(b1$MOD10A == 199),])
> b4_200 = cbindX(b3_200,b3_198,b3_199,c)
> write.csv(b4_200, "2003_snow_198_199_200.csv")
> }