Reputation: 1
I have one problem here. I'm using R to create a Species Distribution Model, but when I try to run the stack line (the climate layers for the model), the feedback of R send me this:
Error in data.frame(values = unlist(unname(x)), ind, stringsAsFactors = FALSE) :
arguments imply differing number of rows: 4, 0
this is my code:
options(java.parameters = "-Xmx1g")
library(raster)
library(dismo)
setwd("C:/Tesis_MDE")
occs <- read.csv("./Registros_CASGMII.csv")
View(occs)
layers<- stack(list.files("./M/presente1","*.asc$",full.names=T))
It would very nice if you could help me, I'm desperate!! haha
Upvotes: 0
Views: 1527
Reputation: 3
Late to this post, but I felt this error message wasn't clear when I received it, so I am posting my explanation.
In this case, the error is raised due to an improper use of utils::stack
.
unnamed.list <- list(c(1,2,3), c(4,5), c(6))
print(unnamed.list)
#> [[1]]
#> [1] 1 2 3
#>
#> [[2]]
#> [1] 4 5
#>
#> [[3]]
#> [1] 6
# this will error
stack(unnamed.list)
#> Error in data.frame(values = unlist(unname(x)), ind, stringsAsFactors = FALSE): arguments imply differing number of rows: 6, 0
# this will work fine
names(unnamed.list) <- seq_along(unnamed.list)
stack(unnamed.list)
#> values ind
#> 1 1 1
#> 2 2 1
#> 3 3 1
#> 4 4 2
#> 5 5 2
#> 6 6 3
# list.files doesn't even return a list, rather a character vector...
# this will error
stack(LETTERS)
#> Error in data.frame(values = unlist(unname(x)), ind, stringsAsFactors = FALSE): arguments imply differing number of rows: 26, 0
Created on 2023-09-26 with reprex v2.0.2
Your error indicates that list.files returned a character vector with 4 elements, and stack
didn't know what to do since it's first argument must be a list or data.frame. See ?utils::stack
.
Upvotes: 0
Reputation: 41
I cannot provide an answer (and I dont have enough reputation to just comment), because I cannot read your data to check what their structure is etc. The recommendation is to provide a reproducible example: https://stackoverflow.com/help/minimal-reproducible-example. I stumbled over your question because I had the same error message, but for me it was as simple (and embarrassing) as having forgotten to attach the library(raster). The error message might in this case just tell you that the command could not find the data - so maybe your issue is actually with the referring to the data to be read in? What does
list.files("./M/presente1","*.asc$",full.names=T)
actually give you?
Upvotes: 2