-1

I have this dataframe:

df <- structure(list(a = c(2, 5, 90, 77, 56, 65, 85, 75, 12, 24, 52, 32), b = c(45, 78, 98, 55, 63, 12, 23, 38, 75, 68, 99, 73), c = c(77, 85, 3, 22, 4, 69, 86, 39, 78, 36, 96, 11), d = c(52, 68, 4, 25, 
79, 120, 97, 20, 7, 19, 37, 67), e = c(14, 73, 91, 87, 94, 38, 1, 685, 47, 102, 666, 74)), class = "data.frame", row.names = c(NA, -12L))

and this script:

sqmat <- structure(c(12, 68, 1, 31, 5, 7, 25, 17, 1, 25, 17, 1, 31, 5, 
7), .Dim = c(3L, 5L))

sqmat_2 <- structure(c(52, 4, 1, 31, 26, 22, 52, 4, 1, 23, 55, 12, 31, 26, 
22), .Dim = c(3L, 5L))

I need an "if-else" condition:

If the output row=1, col=1 in sqmat_2 (52) is > then twice output row=1, col=1 in sqmat (2 x 12) => give me "O". Else (if 52 <= 2 x 12) give me "C".

Point by point of the two matrices sqmat and sqmat_2; and display the results in a matrix. How can I do? Thanks for helping me!

bcsfh
  • 137
  • 5
  • 2
    (1) What is `sq_3`? You don't define it here. (2) Not exactly sure if your `Y1,1` translates to `sq_2[[1]][1,1]` or something else. (3) Is there a reason we need to go through both sets of double-`for` loops to get to two lists of matrices? It would be far simpler to give the raw data using `dput(sq)` --> `sq <- list(structure(c(12, 68, 1), .Dim = c(3L, 1L)), structure(c(31, 5, 7), .Dim = c(3L, 1L)), structure(c(25, 17, 1), .Dim = c(3L, 1L)), structure(c(25, 17, 1), .Dim = c(3L, 1L)), structure(c(31, 5, 7), .Dim = c(3L, 1L)))` – r2evans Apr 02 '22 at 11:57
  • Sorry about sq_3, I edited the question. About the double-for loops that give the two matrices, I don't know a different way to do it. – bcsfh Apr 02 '22 at 12:03
  • If we don't need to see how you generate an object, as I demonstrated, use `dput(sq)` and `dput(sq_2)`, pasting the output into a code block. See https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info for demonstration of that (other than my first comment that included one of your objects already). – r2evans Apr 02 '22 at 12:34
  • But I think you can still explain what you mean by `X1,1` and `X3,5` and such, when your data is a `list` of `matrix` elements, each of which is 3 rows and 1 column. Perhaps if you show the actual output and then you hand-edit that output to show what you are trying to accomplish (*expected output*). – r2evans Apr 02 '22 at 12:35
  • Thx r2evans! I hope is clearer now – bcsfh Apr 02 '22 at 14:07

1 Answers1

0

Do you mean this?

ifelse(sqmat_2 > (2*sqmat), "O", "C")
#      [,1] [,2] [,3] [,4] [,5]
# [1,] "O"  "C"  "O"  "C"  "C" 
# [2,] "C"  "O"  "C"  "O"  "O" 
# [3,] "C"  "O"  "C"  "O"  "O" 
r2evans
  • 141,215
  • 6
  • 77
  • 149