-2

I have a data matrix denoted by A of order 24*1828, I want to calculate the mean absolute error between each value of the matrix A with the last value, for example in first row I want to calculate mean absolute error between each A[1,1],...,A[1,1827] to A[1,1828] and then find the minimum value of the above vector obtained, similarly I repeat the above process for all 24 rows. Please help how I could this in using for loop. Regard

  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. A smaller matrix than your actual matrix should work just fine for testing. – MrFlick Jul 05 '21 at 06:46
  • mae<- mean(abs(a1-a12))** I want to take each value of the **1st** row value with the last value of **1st** row and calculate the **mea** and the find the minimum value of the vector obtained, similar process is repeated for the row of the matrix **A**. – faheem jan Jul 05 '21 at 07:16
  • I don't understand the *minimum* part. In the 1st row you want to compute the absolute differences between each value and the last one. Then compute the mean of those values. This will give *one* value only, not a vector. – Rui Barradas Jul 05 '21 at 07:51
  • I want to calculate the absolute deviation as its a single value so their mean will remain the same as the absolute deviation so when we do this with all values in the first, so in this way we obtain a vector of the absolute deviation and in such a vector we calculate the minimum value. So I want to repeat the same process for all other rows of the matrix **A**. – faheem jan Jul 05 '21 at 07:58

1 Answers1

0

Here is a one-liner with apply.

# Test data
set.seed(2021)
(A <- matrix(sample(48), ncol = 6))

apply(A, 1, function(x) mean(abs(x[-NCOL(x)] - x[NCOL(x)])))
#[1] 11.6 14.4 24.2 18.8 16.2 17.4 23.0 16.0

Edit

After the discussion in comments, here is what I believe is asked.
Function absdev computes the absolute differences between the last element of each row and the other elements of that vector. Then function STAT, whose default is min, is applied to the absolute differences.

absdev <- function(x, STAT = min) {
  f <- match.fun(STAT)
  f(abs(x[-NCOL(x)] - x[NCOL(x)]))
}
apply(A, 1, absdev)
#[1]  2  5 10  5  6  3 14  2
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Thanks really works, please help how we can do this for the second row of the matrix **A** mean which changes it will require. – faheem jan Jul 05 '21 at 07:39
  • @faheemjan This computes the MAE's for all the rows, that's what `apply` does, it's a `for` loop in disguise. No changes are required. – Rui Barradas Jul 05 '21 at 07:40
  • thanks for your interest and time, but I am interested in each separately that suppose that for the first row of the matrix **A** take each value separately and calculate the mean absolute deviation with last value of the first row of the matrix **A**, similarly repeat the process for the remaining rows of the matrix **A**. – faheem jan Jul 05 '21 at 07:51
  • @faheemjan You want to output a vector of absolute differences per row? – Rui Barradas Jul 05 '21 at 07:53
  • No, I want the absolute deviation of each value with the last value of the same row. so each deviation gives a single value and combined a vector and then I want to calculate minimum value in this vector. – faheem jan Jul 05 '21 at 08:04
  • @faheemjan See if the edit does what you are asking. – Rui Barradas Jul 05 '21 at 08:22