4

I currently possess two rasters with the EXACT SAME resolution and extent, and yet they show different number of rows when loaded into the global environment in R.

In addition, before these rasters were made a dataframe in R by using the asctodataframe() function in R, they were initially stacked and the PCA was calculated using the rasterPCA() function, and then, I obtained the rasters in the snapshot shown below.

enter image description here

However, the individual rasters were loaded as a large Rasterlayer and a formal class raster layer. What is the difference between the two? And is this difference causing a change in the number of rows when loaded into R.

Snapshot of Individual rasters:

enter image description here

For more info: please see Same extent and resolution of rasters, but different number of cells


library(raster)
library(RStoolbox)

###################################
##Loading the Present day variables and then creating a raster stack, followed by a PCA
##################################

bio3 <- raster("C:\\Users\\rameshv\\4_ForR\\bio3")
bio4 <- raster("C:\\Users\\rameshv\\4_ForR\\bio4")
bio5 <- raster("C:\\Users\\rameshv\\\4_ForR\\bio5")

pres_stack <- stack(bio3, bio4,bio5)

pre_pca <-  rasterPCA(pres_stack, nComp = 2) #Choosing the first two axes

#Note: YOU NEED TO CALL the right element, else it will not writeRaster
writeRaster(pre_pca$map,"C:\\Users\\rameshv\\Downloads\\5_PCAforR\\PC.asc", format="ascii", bylayer=T)

#################################
##Loading the LGM variables and then creating a raster stack, followed by a PCA

lg3 <- raster("C:\\Users\\rameshv\\4_ForR\\cclgmbi3")
lg4 <- raster("C:\\Users\\rameshv\\4_ForR\\cclgmbi4")
lg5 <- raster("C:\\Users\\rameshv\\4_ForR\\cclgmbi5")


lg_stack <- stack(lg3,lg4,lg5)

lg_pca <- rasterPCA(lg_stack, nComp = 2) #Choosing the first two axes

#Note: YOU NEED TO CALL the right element, else it will not writeRaster
writeRaster(lg_pca$map,"C:\\Users\\rameshv\\5_PCAforR\\PC.asc", format="ascii", bylayer=T)

#########Now I have two rasters that are PC1 and PC2 of the variables    chosen and shall run the multivariate code provided by Hamann et al., 2015

library(SDMTools)     # install package to read and write ESRI ASCII grids
library(yaImpute)     # install package for k-nearest neighbour (kNN) search

lg1 <- asc2dataframe("C:\\Users\\rameshv\\5_PCAforR\\PC_1.asc") 
lg2 <- asc2dataframe("C:\\Users\\rameshv\\5_PCAforR\\PC_2.asc")
present1  <- asc2dataframe("C:\\Users\\rameshv\\5_PCAforR\\PC_1.asc")
present2  <- asc2dataframe("C:\\Users\\rameshv\\5_PCAforR\\PC_2.asc")

> str(lg1)
 'data.frame':   44352 obs. of  3 variables:
  $ y    : num  2209806 2209806 2209806 2209806 2209806 ...
  $ x    : num  -5265209 -5260209 -5250209 -5245209 -5240209 ...
  $ var.1: num  -260 -252 -214 -198 -187 ...
   - attr(*, "filenames")=List of 2
  ..$      : chr "C:\\Users\\rameshv\\Downloads\\Climate Stability\\Data_LGM_Present\\LGM\\5_PCAforR\\PC_1.asc"
  ..$ names: chr "var.1"

  > str(present1)
   'data.frame':   44340 obs. of  3 variables:
    $ y    : num  2209806 2209806 2209806 2209806 2209806 ...
    $ x    : num  -5265209 -5260209 -5250209 -5245209 -5240209 ...
    $ var.1: num  -38.26 -32.95 -8.26 3.47 9.82 ...
    - attr(*, "filenames")=List of 2
    ..$      : chr "C:\\Users\\rameshv\\Downloads\\Climate Stability\\Data_LGM_Present\\Present\\5_PCAforR\\PC_1.asc"
    ..$ names: chr "var.1"
0

1 Answer 1

3
+50

First, please look at the objects in R and not RStudio, or whatever IDE you are using. There have been continuing issues with spatial objects in RStudio and I do not consider it reliable. It would also be helpful if you provided your code so we could track how each object was created.

I took a look at the source code for the rasterPCA function and the Large RasterLayer object class is not being assigned in the function. I am wondering if the object name is coming from RStudio. I would not worry because I think that these objects are still formally RasterLayer and RasterStack objects.

The difference in number of rows, columns, values (from your previous post) could be attributed to the spatial object information not displaying correctly in the IDE or the function not assigning the values correctly. The rasterPCA function is; 1) taking a sample, 2) predicting to the raster values, 3) assigning the predicted values back to the raster. The code looks sound and when I have used it, have observed no problems. Keep in mind that the function could be producing NA values that also may effect the displayed number of values in the raster. The critical element is that the number of rows, columns and geographic extent do not change.

3
  • Based on your suggestion, I have attached the code. The issue is arising once I use the asc2dataframe function I think? You are right that the rasterPCA function seem sound. In fact, I excluded the rasterPCA function and proceeded to convert the rasters to a dataframe, without taking the PC values, and still I see a difference in number of rows. And yes, I have been using RStudio so far and haven't had any issue. Mar 13, 2017 at 15:58
  • Just tried it in R, and Updating it above. Mar 13, 2017 at 16:14
  • The issue looks related to asc2dataframe and not raster. Just coerce the pca stack directly using getValues. This will result in a data.frame with a column for each raster and skip writing out, then reading back in the raster. I do believe that the difference in values is attributed to NA's. Mar 13, 2017 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.