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

improve factor handling #18

Closed
rhijmans opened this issue Jul 19, 2022 · 1 comment
Closed

improve factor handling #18

rhijmans opened this issue Jul 19, 2022 · 1 comment
Assignees
Labels
invalid This doesn't seem right

Comments

@rhijmans
Copy link
Contributor

rhijmans commented Jul 19, 2022

Hi, In sample_strat you have

    samples <- samples %>%
      dplyr::left_join(samples, sraster_cats, by = c("strata" = "value"))

Where

    sraster_cats <- cats(sraster) %>% 
      as.data.frame()

Which assumes that the name of the first column of sraster_cats is always "value". That is not correct. I think you should either use the actual column name, or, perhaps easiest, change it to "value" like this.

    sraster_cats <- cats(sraster) %>% 
      as.data.frame()
   colnames(sraster_cats)[1] <- "value"

I found this because "sgsR" triggered a reverse dependency error with "terra". I was able to get rid of that error for your example; but your assumption about the column name is still wrong.

@tgoodbody tgoodbody self-assigned this Jul 19, 2022
@tgoodbody tgoodbody added the invalid This doesn't seem right label Jul 19, 2022
@tgoodbody
Copy link
Owner

Hi @rhijmans

Thanks for letting me know about this and sorry for taking up your time.

Ive used your code suggestion and tested outputs with no errors.

Appreciate your time

T

  if(!is.null(terra::cats(sraster)[[1]])){
    message("'sraster' has factor values. Converting to allow mapping.")
    
    #--- change suggested by R Hijmans ---#
    sraster_cats <- cats(sraster) %>% 
      as.data.frame()
    colnames(sraster_cats)[1] <- "value"
    
  }

@tgoodbody tgoodbody reopened this Jul 19, 2022
tgoodbody added a commit that referenced this issue Jul 19, 2022
tgoodbody added a commit that referenced this issue Jul 19, 2022
* `enhanced` - `sample_strat()` - added parameter `method` that allows users to choose between `"Queinnec"` (default method implemented in previous sgsR versions) and `"random"` (stratified random sampling). The random method ignores much of the functionality of the algorithm to allow users to use standard stratified random sampling approaches without the use of a focal window to locate contiguous stratum cells.

* `fixed` - `sample_strat()` factor handling improvement - GitHub issue #18

* `enhanced` -  `calculate_allocation()` improved documentation for output data frame to make attributes more clear.

* `fixed` - `calculate_representation()` will now not plot bar chart twice & `NA` values in existing will not be removed.

* `fixed` - `existing` samples with other attributes will now not break sampling using `sample_ahels() / sample_clhs()` if values are `NA`. Variables are also added back to the sample output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants