# Calculating the difference between averages in of rows in a matrix

Hi, I am new to R. I have a matrix of 10 rows and 20 columns that represents vegetation data collected from 10 sites over a 20 year period. Each column represents a year for each of the 10 sites. I would like to assess changes between two periods (1-10 and 11-20) by calculating the difference between averages of the beginning (1-10) and end (11-20). I would then like to calculate the standard deviation as a function of the severity of these changes. I hope this makes sense. I would like to create a script that does the following:

1. Calculate the mean and standard deviation for each row of the first 10 years (mean and standard deviation of rows from amp.1 to amp.10)

2. Calculate the mean and standard deviation for each row of the remaining 10 years (mean and standard deviation of rows from amp.11 to amp.20). Note that columns 19 and 20 contain NAs but they should still be used in the calculation of the mean and sd.

3. Subtract the mean in 1 from the mean in 2 (2-1) and subtract the sd in 1 from the sd in 2.

4. Final output is a matrix with 10 rows and names of the following columns: mean 1-10, sd 1-10, mean 11-20, sd 11-20, a column of the difference of the means, and column of the difference of the sd. I am hoping to use the 'difference of the sds' column as a function to scale the severity of the changes obtained in the 'difference of the means' column. Not sure this is right but I would like to show how severe/widespread the changes estimated from the 'difference of the means' column are, over 20 years using sd.

I have provided a reprex of the dataset.

Edward

``````library(reprex)

#>           amp.1      amp.2      amp.3      amp.4     amp.5     amp.6     amp.7
#> [1,] 0.06355208 0.01929583 0.05010625 0.07260833 0.1451646 0.2267583 0.1553229
#> [2,] 0.04786250 0.06164375 0.07718125 0.12465000 0.1366375 0.3186677 0.2299771
#> [3,] 0.10436250 0.11362484 0.08359375 0.07995208 0.1058729 0.1818938 0.1076958
#> [4,] 0.19988125 0.29163542 0.14515000 0.15135313 0.1347437 0.2632969 0.2258917
#> [5,] 0.09733750 0.31862083 0.14447500 0.16182708 0.2022292 0.2894500 0.1968000
#> [6,] 0.15979063 0.23393229 0.12570625 0.11101667 0.1901313 0.2722760 0.1594500
#>           amp.8     amp.9     amp.10     amp.11     amp.12     amp.13    amp.14
#> [1,] 0.09265000 0.1314458 0.12417917 0.22735000 0.07611458 0.06754583 0.0309750
#> [2,] 0.08024375 0.1006833 0.12896875 0.09196010 0.07724167 0.07230521 0.0399625
#> [3,] 0.07395422 0.1165167 0.05685625 0.08634375 0.09019375 0.15305208 0.1393375
#> [4,] 0.16513542 0.2381979 0.11930000 0.26552500 0.21667708 0.23057917 0.2556229
#> [5,] 0.23513793 0.2586385 0.18660417 0.23920625 0.14387292 0.15499583 0.1608542
#> [6,] 0.21845833 0.2316521 0.14089375 0.28081250 0.19513542 0.13513750 0.1778271
#>          amp.15     amp.16     amp.17     amp.18 amp.19 amp.20
#> [1,] 0.11179479 0.03830000 0.01898021 0.16301042     NA     NA
#> [2,] 0.18201907 0.05831875 0.09390000 0.18453646     NA     NA
#> [3,] 0.09447812 0.08624375 0.11254063 0.08977708     NA     NA
#> [4,] 0.17393333 0.15855208 0.16844375 0.14920625     NA     NA
#> [5,] 0.15920625 0.17954583 0.15765417 0.14417500     NA     NA
#> [6,] 0.15242396 0.14681875 0.26499271 0.15425000     NA     NA

View(ampdata)
``````

Created on 2021-02-04 by the reprex package (v1.0.0)

Is this the kind of thing you are looking for?

``````ampdata <- matrix(rnorm(200), nrow = 10, ncol = 20)
StatFunc <- function(ROW) c(Avg = mean(ROW, na.rm = TRUE), SD = sd(ROW, na.rm = TRUE))

Stats1_10 <- apply(ampdata[, 1:10], MARGIN = 1, StatFunc)
Stats11_20 <- apply(ampdata[, 11:20], MARGIN = 1, StatFunc)
Compare <- cbind(t(Stats1_10), t(Stats11_20))
CompareDf <- as.data.frame(Compare)
colnames(CompareDf) <- c("Avg1_10", "SD1_10", "Avg11_20", "SD11_20")
CompareDf\$Diff_Avg <- CompareDf\$Avg11_20 - CompareDf\$Avg1_10
CompareDf\$Diff_SD <- CompareDf\$SD11_20 - CompareDf\$SD1_10
CompareDf
#>        Avg1_10    SD1_10    Avg11_20   SD11_20    Diff_Avg     Diff_SD
#> 1  -0.10742381 1.2386940 -0.23358021 1.0289721 -0.12615640 -0.20972192
#> 2   0.55975022 0.5555881  0.05470113 0.8716465 -0.50504909  0.31605842
#> 3  -0.13156136 0.7571076 -0.07316358 0.6458334  0.05839778 -0.11127415
#> 4  -0.08632233 0.7942769  0.02204668 1.3161395  0.10836902  0.52186260
#> 5  -0.25009794 0.7803211  0.10328539 0.6403801  0.35338332 -0.13994107
#> 6  -0.14750191 0.5844020 -0.29283699 0.9095049 -0.14533507  0.32510297
#> 7  -0.33394007 0.9220302  0.30075361 0.5737077  0.63469368 -0.34832246
#> 8   0.03631346 1.5700959 -0.29293289 0.8409346 -0.32924635 -0.72916132
#> 9  -0.12535320 1.1936277 -0.59826611 1.1039655 -0.47291291 -0.08966222
#> 10 -0.08582624 1.0467690  0.36861125 0.9383578  0.45443748 -0.10841114
as.matrix(CompareDf)
#>           Avg1_10    SD1_10    Avg11_20   SD11_20    Diff_Avg     Diff_SD
#>  [1,] -0.10742381 1.2386940 -0.23358021 1.0289721 -0.12615640 -0.20972192
#>  [2,]  0.55975022 0.5555881  0.05470113 0.8716465 -0.50504909  0.31605842
#>  [3,] -0.13156136 0.7571076 -0.07316358 0.6458334  0.05839778 -0.11127415
#>  [4,] -0.08632233 0.7942769  0.02204668 1.3161395  0.10836902  0.52186260
#>  [5,] -0.25009794 0.7803211  0.10328539 0.6403801  0.35338332 -0.13994107
#>  [6,] -0.14750191 0.5844020 -0.29283699 0.9095049 -0.14533507  0.32510297
#>  [7,] -0.33394007 0.9220302  0.30075361 0.5737077  0.63469368 -0.34832246
#>  [8,]  0.03631346 1.5700959 -0.29293289 0.8409346 -0.32924635 -0.72916132
#>  [9,] -0.12535320 1.1936277 -0.59826611 1.1039655 -0.47291291 -0.08966222
#> [10,] -0.08582624 1.0467690  0.36861125 0.9383578  0.45443748 -0.10841114
``````

Created on 2021-02-04 by the reprex package (v0.3.0)

This worked, thank you.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.