I'm having a strange problem that I can't wrap my head around. I have a function in R that compares two vectors at position x and then inserts the vector into a matrix depending on that condition. Within the function, it replaces the column fine, but if I call the matrix afterward, it returns the original values. I've attached a simplified version of the code below.
## define matrix names and size
r1w <- matrix(c(0), nrow=4, ncol=4)
r1l <- matrix(c(0), nrow=4, ncol=4)
## vectors that should replace column in matrix
b <- c(1, 2, 3, 4)
a <- c(3, 4, 5, 6)
## Function
playgame <- function(x, y, r, g) {
if (x[r] > y[r]) {
(r1w[, g] <- x); r1l[, g] <- y; View(r1w)
} else {
(r1w[, g] <- y); r1l[, g] <- x
}
}
## this returns a changed matrix
playgame(a, b, 1, 1)
## calling afterwards returns original matrix
r1w
If anyone could tell me where I made a mistake I would be forever in your debt.