Home > Mobile >  How can I create the following "if-else" condition?
How can I create the following "if-else" condition?

Time:04-03

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!

CodePudding user response:

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" 
  • Related