I'm trying to make a reclassification of several raster files with R. My raster files contain species ranges with the values of the total range in the occurring cells. The other cells have NoData. The files have their rank as name (1,2,3...). Now I try to reclass the values of the cells where species occurs with the Rank. I tried with for looping the reclassify function without success... thanks in advance!
-
Please provide [a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Thomas Feb 28 '14 at 15:11
-
user3327377 you should provide a better description of your problem following, as much as possible, the example provided by @Thomas. If you think it may be difficult, provide a clear example. Describe a single raster file, what you mean by total range? Each raster describes a single species occurrences (presence/absence)? What the rank means in the context of your problem? Can rank be interpreted as a single species code? – Paulo E. Cardoso Feb 28 '14 at 17:28
1 Answers
Let's say you have one raster representing the presence/absences of a single species. You also know the area extent and the range (expressed as occurrences) of this particular species. For each cell where this species is present you've filled it with the total occurrences.
Lets say this species was detected on 50 out of 400 cells.
The species occurrence expressed by presence on 50 random cells.
e1 <- extent(0,10,0,10)
r1 <- raster(extent(0,10,0,10))
res(r1) <- 0.5
r1[1:ncell(r1)] <- NA
r1[sample(ncell(r1), 50, rep = F)] <- 50
plot(r1)
But you may have this rasterLayer as a image file, stored at your disk and named with a rank of the species.
There are several ways to replace values in a rasterLayer. If for this particular species the rank is 1, you can replace the range by rank with
r1[!is.na(r1)] <- 1
If you want to lop over your file folder try this:
wdata <- '.../R/Stackoverflow/21876858' # your local folder
f.reclass <- function(x=x){
files <- list.files(file.path(wdata), all.files = F) # list all files from a given driver folder
# Assuming TIF images, List files from wdata folder
## Change below if using any other format
ltif <- grep(".tif$", files, ignore.case = TRUE, value = TRUE)
stkl <- stack()
for(i in 1:length(ltif)){
x <- raster(file.path(wdata, ltif[i]),
package = "raster", varname = fname)
rank <- as.numeric(sub("*.tif", "", ltif[i]))
# Change also here if not tif
stopifnot(is.numeric(rank)) # check for numeric
x[!is.na(x)] <- rank
stkl <- addLayer(stkl, x)
}
stkl
}
spreclass <- f.reclass(x=x)
You'll get a rasterStack object with your reclassified rasterLayers. your can unstack them, export, manipulate...

- 5,778
- 32
- 42
-
you assumed perfectly. one raster is the representing the occurrence of a species in the whole world. In the cells where the species occurs i have the have the value of the total range of this species. So lets say one species occurs in 100 cells of 1km^2 it has in each cell where it occurs 100 as a value. If this species is the species with the smallest range size it has the name 1. because its rank is 1. know i would like to change its name from one to 100. I think the script you provided is perfekt but i go one little error in it: Error in as.list(x) :argument "x" is missing, with no default – user3327377 Mar 03 '14 at 09:36
-
its referring to the stack function and i think i have to write: stkl<-stack(files) but then i get the error: Error in rep.int(names(x), lapply(x, length)) : invalid 'times' value...what am i doing wrong here? – user3327377 Mar 03 '14 at 09:51
-
Ah i'm sorry! my computer crashed before and i didn't load the packages...thanks a lot for your help it worked perfectly – user3327377 Mar 03 '14 at 10:13
-
@user3327377 Good to know. Please upvote the answer if it was useful. – Paulo E. Cardoso Mar 03 '14 at 10:15