mcglinnlab / shark-ray-div

Global patterns of shark and ray diversity
0 stars 1 forks source link

dropLayer #32

Closed EmmalineSheahan closed 6 years ago

EmmalineSheahan commented 6 years ago

So in order to use the functions I wrote earlier to conduct the phylogenetic analyses on the regional rasters, I need to create a directory of the species that appear in the two regions. My current issue is that all 534 species are still listed in the stacks for each region because I used the intersect function, even though not all of them actually occur there. I wrote this code in order to drop the species layers from the raster stack where it's NA for every cell (so the species doesn't occur in the region at all) so I can pull out the layer names at the end of it and see which species are there and which aren't:

for (i in 1:6) {
  for (j in 1:534) {
    if (all(is.na(trop_atlantic_stack[[i]][[j]]@data@values))) {
      trop_atlantic_stack[[i]] <- dropLayer(trop_atlantic_stack[[i]], j)
    }
  }
}

but it keeps giving me this error: Error in .local(x, ...) : not a valid subset

I think the problem is in the dropLayer function, because for some reason R was having trouble registering even a number in the place of j even though the documentation for that function says it should be an integer

EmmalineSheahan commented 6 years ago

Dr. Sotka helped me figure out a way to make it work:

# creating dir
trop_atlantic_names <- vector("list", length = 6)
for (i in 1:6) {
  for (j in 1:534) {
    #print(any(trop_atlantic_stack[[i]][[j]]@data@values==1))
    if (sum(trop_atlantic_stack[[i]][[j]]@data@values==1,na.rm=T)>0)
      {
      trop_atlantic_names[[i]][[j]] <- trop_atlantic_stack[[i]][[j]]@data@names
      } else {trop_atlantic_names[[i]][[j]] <- NA}
  }
}

So now I have a list of names of which species are in the realm

dmcglinn commented 6 years ago

Nice! Thanks for the update

On Jun 2, 2018, at 5:55 PM, EmmalineSheahan notifications@github.com wrote:

Dr. Sotka helped me figure out a way to make it work:

creating dir

trop_atlantic_names <- vector("list", length = 6) for (i in 1:6) { for (j in 1:534) {

print(any(trop_atlantic_stack[[i]][[j]]@data@values==1))

if (sum(trop_atlantic_stack[[i]][[j]]@data@values==1,na.rm=T)>0)
  {
  trop_atlantic_names[[i]][[j]] <- trop_atlantic_stack[[i]][[j]]@data@names
  } else {trop_atlantic_names[[i]][[j]] <- NA}

} } So now I have a list of names of which species are in the realm

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.