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.
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:
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"