Home > Mobile >  How do I write codes to calculate the following averages in an array?
How do I write codes to calculate the following averages in an array?

Time:01-07

Apologies if this is too simple. I have been looking but not able to solve it.

Dimensions of my data looks like this

y is [1:267, 1:3, 1:10]

It basically represents 267 sites, 3 surveys every year for 10 years. Whole data is in 0s and 1s

I want to get two different values.

Temporal value

  1. average of each year. Basically, sum of values of 267 sites/ 267*3 The outcome I am expecting should be 10 values for 10 different years.

Spatial

  1. average of all years and surveys at 267 sites. The outcome I am expecting should be 267 values for 267 sites.

This is how I created my array.

y <- array(NA, dim=c(nsite, nsurvey, nyear))
date <- array(NA, dim=c(nsite, nsurvey, nyear))
for (t in 1:nyear) {
  for (j in 1:nsurvey) {
    y[,j,t] <- detect[,3*t-3 j]; date[,j,t] <- dates[,3*t-3 j]
  }
}

I am trying to write this code in R

CodePudding user response:

I wouldn't particularly recommend doing this with pure arrays, as opposed to using long-format data frames with column names or similar. But you can if you wish. Rather than loop over your array you can use apply() and related functions to get the statistics you want out of it. For example, something like this:

# Make reproducible
set.seed(1622)

# Dims
nyear <- 10
nsurvey <- 3
nsite <- 267

# Generate some 0/1 observations
n <- nyear * nsurvey * nsite
vals <- rbinom(n, 1, 0.5)

# Make the array and name the dimensions
y <- array(vals, 
           dim=c(nsite, nsurvey, nyear), 
           dimnames = list(site = 1:nsite, 
                           survey = 1:nsurvey,
                           year = 1:nyear))

dim(y)
#> [1] 267   3  10

# Site 1
y[1,,]
#>       year
#> survey 1 2 3 4 5 6 7 8 9 10
#>      1 0 0 0 0 1 0 0 1 0  1
#>      2 1 0 0 0 0 0 0 1 1  0
#>      3 1 0 1 1 1 1 0 0 1  1

# Site 200
y[200,,]
#>       year
#> survey 1 2 3 4 5 6 7 8 9 10
#>      1 1 0 0 1 0 1 0 1 1  1
#>      2 1 0 0 0 1 1 1 1 0  1
#>      3 1 1 0 1 1 1 1 0 1  1

# Sites 1:5, Year 1
y[1:5,,1]
#>     survey
#> site 1 2 3
#>    1 0 1 1
#>    2 0 0 0
#>    3 1 1 1
#>    4 0 0 0
#>    5 1 0 1

# Site 1, Survey 1
y[1,1,]
#>  1  2  3  4  5  6  7  8  9 10 
#>  0  0  0  0  1  0  0  1  0  1


## Average of all sites and surveys by year
apply(y, "year", mean)
#>         1         2         3         4         5         6         7         8 
#> 0.5106117 0.5243446 0.4794007 0.4968789 0.4968789 0.5168539 0.4818976 0.4881398 
#>         9        10 
#> 0.5230961 0.5156055

## Average of all years and surveys by site
## Just show the first 10 sites
out <- apply(y, "site", mean)
length(out)
#> [1] 267
out[1:10]
#>         1         2         3         4         5         6         7         8 
#> 0.4333333 0.3666667 0.6000000 0.3666667 0.4666667 0.3333333 0.6333333 0.5666667 
#>         9        10 
#> 0.4666667 0.6333333

## You can also do eg
round(apply(y, c("survey", "year"), mean),2)
#>       year
#> survey    1    2    3    4    5    6    7    8    9   10
#>      1 0.52 0.52 0.51 0.53 0.50 0.54 0.50 0.51 0.50 0.57
#>      2 0.50 0.54 0.46 0.48 0.48 0.50 0.49 0.47 0.53 0.50
#>      3 0.51 0.51 0.48 0.48 0.51 0.51 0.46 0.48 0.54 0.48
  •  Tags:  
  • Related