Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to remove this for loop

$
0
0

Source of the program:

gf <- c(2, 1, NA, 4, 5,
        4, 5, NA, 1, 2,
        3,NA, 1, NA, NA)

dim(gf) <- c(5,3)

    n = nrow(gf)
    r = ncol(gf)

    for (i in 1:r) {
        col <- gf[ ,i]
        if (FALSE %in% (c(1:n) == col[col])) {
          cat("Error (i):", i, "\n")
          break
        }
    }
    gf

The matrix represents a game file with a column per round. If x meets y in a game, then y meets x in the same game. Test if the redundancy in the symmetric matrix is consistent.

    Matrix:
      [,1] [,2] [,3]
[1,]    2    4    3
[2,]    1    5   NA
[3,]   NA   NA    1
[4,]    4    1   NA
[5,]    5    2   NA

How to eliminate the for loop and have a pure matrix formula ?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>