I表示下面的hacky替代方案非常慢。有人可以提供更优雅的解决方案吗?
#set up data
x1 = x2 = x3 = x4 = rast( nrows=180, ncols=360, nlyrs=1, xmin=-180, xmax=180, ymin=-90, ymax=90)
x1[] = 1.0
x2[] = F
x3[] = "cat"
x4[] = as.integer(1)
#create multiband raster
r4 = c(x1,x2,x3,x4)
#test for numeric - the second hacky approach works SLOWLY, but hacky
# Are the contents of my raster spooling out into memory?
sapply(r4, is.numeric)
sapply(r4, function(x)is.numeric(x[]))
#test for bool - terra function works
sapply(r4, is.bool)
#test for factor - works
sapply(r4, is.factor)
#test for integer - second approach (terra function) works
sapply(r4, is.integer)
sapply(r4, is.int)
您可以做
sapply(r4[1], is.numeric)
#lyr.1 lyr.1 lyr.1 lyr.1
# TRUE FALSE FALSE TRUE