[R-sig-Geo] stars objects and case_when

Julian M. Burgos ju||@n@burgo@ @end|ng |rom h@|ogv@tn@|@
Tue Sep 15 13:51:48 CEST 2020


Thanks Edzer, I missed that the file name becomes the name of the layer.  This works great.

I should say that I am enjoying converting my raster processing algorithms into the stars package.  It is nice to use tidyverse- and sf-friendly tools.  Thank you (and all your collaborators) for these tools.

My best,

Julian


Edzer Pebesma writes:

> Thank you for pointing to this possibility, I'll add it to the stars
> docs somewhere.
>
> This works but only slightly adapted, e.g. as
>
> out = read_stars(system.file("tif/L7_ETMs.tif", package = "stars")) %>%
>   slice(band, 1) %>%
>   setNames("x") %>%
>   mutate(x = case_when (x<100 ~ NA_real_,   x>100 & x<150 ~ 2))
>
> where:
> - I uses setNames("x") so that the attribute is renamed to "x", and
>   you can use x in the case_when expressions (rather than the lengthy
> "L7_ETMs.tif")
> - I chnaged NA into NA_real_ : the first RHS of the formulas need to
>   be all of the same type; as typeof(NA) is "logical", it breaks on
>  the second RHS which returns a numeric; if you would TRUE or FALSE
> rather than 2 in the second case_when formula, using NA would be the
> right thing to do in the first.
>
> The examples of case_when document the need for typed NA in RHS: this
> is intended behavior.
>
>
> On 9/14/20 4:39 PM, Julian M. Burgos wrote:
>> Dear list,
>> I am wondering if there is a way to use logical statements to
>> replace values of a stars object, for example the case_when function
>> or some other "tidyverse friendly" approach.  For example, I can do
>> something like this:
>> st1 <- read_stars(system.file("tif/L7_ETMs.tif", package = "stars"))
>> %>%
>>    slice(band, 1)
>> st1[st1 < 100] <- NA
>> st1[st1 > 100 & st1 < 150] <- 2
>> ... and so for.  But I am wondering if there is a way to do this as
>> part of a pipe, looking something like this:
>> st1 <- read_stars(system.file("tif/L7_ETMs.tif", package = "stars"))
>> %>%
>>    slice(band, 1) %>%
>>    mutate(x <- case_when (x<100 ~ NA,
>>                           x>100 & x<150 ~ 2))
>> Any ideas?
>> Takk,
>> Julian
>> --
>> Julian Mariano Burgos, PhD
>> Hafrannsóknastofnun, rannsókna- og ráðgjafarstofnun hafs og vatna/
>> Marine and Freshwater Research Institute
>> Botnsjávarsviðs / Demersal Division
>>    Fornubúðir 5, IS-220 Hafnarfjörður, Iceland
>> http://www.hafogvatn.is/
>> Sími/Telephone : +354-5752037
>> Netfang/Email: julian.burgos using hafogvatn.is
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo using r-project.org
>> https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=02%7C01%7C%7C46229147e3f34abe731108d858de7a97%7C8e105b94435e4303a61063620dbe162b%7C0%7C1%7C637357059855429791&sdata=rUuCmwP21cqjEr%2BMGcDswcFsG4duKfgkcoNwPSflmeM%3D&reserved=0
>>


--
Julian Mariano Burgos, PhD
Hafrannsóknastofnun, rannsókna- og ráðgjafarstofnun hafs og vatna/
Marine and Freshwater Research Institute
Botnsjávarsviðs / Demersal Division
  Fornubúðir 5, IS-220 Hafnarfjörður, Iceland
www.hafogvatn.is
Sími/Telephone : +354-5752037
Netfang/Email: julian.burgos using hafogvatn.is



More information about the R-sig-Geo mailing list