1

I am currently trying to extract rainfall data from Mexico from the CHIRPS database. The goal is to get a comprehensive database of monthly rainfall over a period of 15 years. This involves merging a lot of columns after extracting the data from .tif-files which contain the information on weather conditions in a specific months.
CVE_ENT and CVE_MUN are the two variables that later on help me to identify individual municipalities.

Running my code in R and looking at the resulting data frame everything looks fine. However, as soon as I try to extract it to become a .dta or .csv file, I get the following error message: fwrite(vextractall, file="rainfall55.csv") Error in fwrite(vextractall, file = "rainfall55.csv") : Column 4's length (1) is not the same as column 1's length (2456)

The error occurs for multiple other scenarios e.g. if using write.dta.

Anyone who possibly knows on what I am missing out on? Thanks a lot in advance.

#Data get methods#
tif.raster1 <- raster('chirps-v2.0.1999.01.tif')
crs.LL <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84" 
tif.raster1 <- projectRaster(tif.raster1, crs = crs.LL)
mexico2shp <- readOGR(dsn='GIS Mexican Municipalities', layer='Mexican      Municipalities')
tif.raster1 <- crop(tif.raster1, extent(mexico2shp))
vras.tif1 <- velox(tif.raster1)
iters <- nrow(mexico2shp)
#mapping function#
vextractall <- vras.tif1$extract(mexico2shp, fun=mean)
mexicomm <- as.data.frame(mexico2shp)
vextractall <- as.data.frame(vextractall)
iters <- nrow(mexico2shp)
x <- foreach(a=1:iters) %do% {
if(is.na(vextractall[a,1])) {
 ext <- raster::extract(tif.raster1, mexico2shp[a,], fun=mean)
 vextractall[a,1] <- ext[1,1]
 }
}
vextractall<-as.data.frame(vextractall)
vextractall$CVE_ENT <- mexicomm[,c("CVE_ENT")]
vextractall$CVE_MUN <- mexicomm[,c("CVE_MUN")]
vextractall<-vextractall[,c(ncol(vextractall), 1:(ncol(vextractall)-1))]
vextractall<-vextractall[,c(ncol(vextractall), 1:(ncol(vextractall)-1))]
vextractall <- plyr::rename(vextractall, c("V1"="Milimeters011999"))

tif.raster1 <- raster('chirps-v2.0.1999.02.tif')
tif.raster1 <- crop(tif.raster1, extent(mexico2shp))
vras.tif1 <- velox(tif.raster1)
crs.LL <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84" 
tif.raster1 <- projectRaster(tif.raster1, crs = crs.LL)
vextract2 <- vras.tif1$extract(mexico2shp, fun=mean)
vextract2 <- as.data.frame(vextract2)
iters <- nrow(mexico2shp)
foreach(a=1:iters) %do% {
if(is.na(vextract2[a,1])) {
ext <- raster::extract(tif.raster1, mexico2shp[a,], fun=mean)
vextract2[a,1] <- ext[1,1]
}
 }
vextractall<-as.data.frame(vextractall)
vextract2<-as.data.frame(vextract2)
vextractall$Milimeters021999 <- vextract2
Verena W.
  • 11
  • 2
  • 1
    It is hard to tell what is going on here, but it sounds as if column 4 might be of type `list` - try `str(vextractall)` to see. If it is a list, you could try something like `vextractall[,4] <- unlist(vextractall[,4])`. If that fails you will need to look at it more closely to see how to extract the data you want in the right format. – Andrew Gustar May 06 '17 at 15:03
  • 1
    Thanks a lot for your response. I've tried it (hope<-str(vextractall)) and it gets reported as being NULL. Any thoughts? – Verena W. May 06 '17 at 15:29
  • 1
    If you do that `hope` will always come back as `NULL`. What if you just enter `str(vextractall)` in the console? – Andrew Gustar May 06 '17 at 15:59
  • 1
    I see you've added some extra code. the last line looks odd - you are assigning a whole dataframe `vextract2` to a single column of `vextractall`. That might be causing the problem. – Andrew Gustar May 06 '17 at 16:03
  • 1
    Yes I am very sorry I forgot to add this bit in the beginning. This is how I try to add on the data I have extracted from the file for the next month of rainfall data. vextractall originally has three columns, two to identify the municipality and one for the rainfall records. How would you go about it? – Verena W. May 06 '17 at 16:05
  • 2
    If the municipalities are in the same order (which it looks as if they will be) then you can just do `vextractall$Milimeters021999 <- vextract2$V1`, assuming the rainfall data column is `V1` as it was in the first section. – Andrew Gustar May 06 '17 at 16:10
  • 1
    Yes they are in order. But thanks a lot! It seems to work. I've only run the first few months, but results looked good and I was able to extract the data. Can I ask why extracting the one column from a one column vector actually matters in this case? – Verena W. May 06 '17 at 16:31
  • 2
    A dataframe is effectively a list of vectors. Because you have defined it as a dataframe, R wraps the single column in a list. `vextract2` is a list with one element, called `V1`. `vextract2$V1` is the vector you want. I agree it seems overkill for a single column, but that's just the way it works. – Andrew Gustar May 06 '17 at 16:40

0 Answers0