library(rgdal)
my_asc = dir("~开发者_如何学Python/Pulpit/dods/karol/TVDI113_121",
pattern=".asc", recursive=TRUE, full.names=TRUE)
for (i in 1:length(my_asc)) {
r <- readGDAL(my_asc[i])
z <- as.matrix(r)
vectordata[i] <- mean(z)
vectordatamax[i] <- max(z)
vectordatamin[i] <- min(z)
vectordev[i] <- sd(z, na.rm=TRUE)
hist(z)
png(filename="hist"+tostring(i)+".png")
}
I try to do some modification of this loop, but it still doesn't works (I am working under Rstudio) - which fragment is incorrect?
I would like also to use more complicated pattern (to list only files which contains on the end of it name two numbers), but adding something like:
pattern="_??.asc"
seems not works.
I would like to add one more loop to get list of folders (instead of manually inserting directories into my_asc variable), but I haven't got Idea how I can do it? I do not know, why my way of creating vectors for mean, max, min and standard deviation values is not working...
Where to start . . .
You probably want na.rm = TRUE for each of mean, max, and min, and you will need to enter TRUE correctly for sd.
The hist(z) should come after the png(filename, ...) and that will have to be followed by a dev.off() (at least).
You cannot paste together strings with "+" in R, use paste().
vectordata <- vectordatamax <- vectordatamin <- vectordev <- numeric(length(my_asc))
for (i in seq_along(my_asc)) {
r <- readGDAL(my_asc[i])
## as.matrix is not necessary, as the band values are accessible directly
##z <- as.matrix(r)
z <- r[[1]]
vectordata[i] <- mean(z, na.rm=TRUE)
vectordatamax[i] <- max(z)
vectordatamin[i] <- min(z)
vectordev[i] <- sd(z, na.rm=TRUE)
png(filename=paste("hist", i, ".png", sep=""))
hist(z)
dev.off()
}
精彩评论