2

Do somebody know what is wrong with my code? I edited the post, because i didn´t give you the data. I want to calculate the sd. The calculation of the mean worked.

Here is the link to the cropped data:

https://drive.google.com/drive/folders/1ljT1fzaDlSmn_3j7zHshS5lrV1wBvVQD

library(raster)
r <- brick("filename")
#mean
mean <- mean(r)

#sd
standard_dev <- sd(r)
standard_dev2 <- sd(r, na.rm =TRUE)
standard_deviation <- calc(r, sd)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • https://stats.stackexchange.com/questions/171971/how-can-i-calculate-standard-deviation-step-by-step-in-r/171980 – maydin Jun 12 '20 at 12:21
  • `standard_deviation <- sd(r)` – jogo Jun 12 '20 at 12:29
  • 3
    Please read the posting instructions at the top of the [tag:r] tag. In particular provide complete reproducible code and input so anyone else can copy and paste the code in your post into R and see the same result. – G. Grothendieck Jun 12 '20 at 12:32
  • 1
    standard_dev <- sd(r) Fehler in as.double(x) : cannot coerce type 'S4' to vector of type 'double' > standard_dev2 <- sd(r, na.rm =TRUE) Fehler in as.double(x) : cannot coerce type 'S4' to vector of type 'double' – catherine Jun 12 '20 at 12:39
  • 1
    We may be able to help you further if you provide us with a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Eric Jun 12 '20 at 12:46

2 Answers2

3

You want the compute the sd for each cell in a RasterBrick.

Here is a self-contained, minimal reproducible example

library(raster)
b <- brick(system.file("external/rlogo.grd", package="raster"))

Solution

x <- calc(b, sd)

x
class      : RasterLayer 
dimensions : 77, 101, 7777  (nrow, ncol, ncell)
resolution : 1, 1  (x, y)
extent     : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
source     : memory
names      : layer 
values     : 0, 38.5746  (min, max)

It seems that this does not work for you because you have a RasterLayer called sd

sd <- b
calc(b, sd)
#Error in (function (classes, fdef, mtable)  : 
#  unable to find an inherited method for function ‘calc’ for signature ‘"RasterBrick", "RasterBrick"’

In that case you can be more explicit and use the functions namespace (stats)

calc(b, stats::sd)

And then it works again as expected.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
0

Function: sd(x, na.rm = FALSE)

This function computes the standard deviation of the values in x. If na.rm is TRUE then missing values are removed before computation proceeds.

Arguments

x: a numeric vector or an R object but not a factor coercible to numeric by as.double(x).

na.rm: logical. Should missing values be removed?

Example

sd(1:2) ^ 2

Taken from accessing the help documentation in RStudio using:

?sd()

For your situation:

standard_deviation <- sd(r)

We may be able to help you further if you provide us with a reproducible example.

Eric
  • 2,699
  • 5
  • 17