Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract(fun = table) returns only two columns, with matrix in second column #1294

Closed
AramburuMerlos opened this issue Sep 26, 2023 · 1 comment

Comments

@AramburuMerlos
Copy link
Contributor

When using extract() with fun=table to tabulate raster values for each polygon (and raw=FALSE) I get a data.frame with two columns, in which the second column is a matrix, rather than a data.frame with as many columns as raster classes +1.
See example below

> library(terra)
terra 1.7.46
> 
> f <- system.file("ex/lux.shp", package="terra")
> v <- vect(f)
> z <- rast(v, resolution=.01, names="test")
> values(z) <- as.factor(sample(letters[1:5], ncell(z), replace = TRUE))
> 
> e <- extract(z, v, fun = "table", ID=TRUE, raw = FALSE)
> e
   ID  a  b  c  d  e
1   1 75 73 71 90 80
2   2 62 49 41 65 54
3   3 75 58 60 67 65
4   4 23 20 16 16 20
5   5 80 60 62 51 79
6   6 47 44 46 49 53
7   7 25 30 32 35 36
8   8 51 43 56 64 43
9   9 42 49 43 48 49
10 10 68 67 59 54 62
11 11 54 72 64 55 54
12 12 54 70 50 62 58
> ncol(e)
[1] 2
> class(e[,2])
[1] "matrix" "array" 
AramburuMerlos added a commit to AramburuMerlos/terra that referenced this issue Sep 27, 2023
To fix issue rspatial#1294
when using extract() with fun = table and exact = FALSE, the output was a 2-column data.frame, with a matrix in the second column.
This should fix the issue.
@rhijmans
Copy link
Member

fixed with #1298

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants