I'll preface this by saying I'm an R noob and that I think this may have an easy solution, but I'm struggling to find it.
I've got a matrix with 2 columns and 1,000 rows. Keeping the rows fixed, I'd like to create a new variable that randomly chooses one of the elements from the 2 columns. For example making a simple matrix:
matrix(c(1,1,4,6,1,3,2,1,1,7), ncol=2)
[,1] [,2] [,3]
[1,] 1 3 3
[2,] 1 2 1
[3,] 4 1 4
[4,] 6 1 1
[5,] 1 7 7
In the simplified matrix above, the 3rd column (which I just added by hand) just contains a random element from either of the prior columns in the corresponding row. My question is, how would I create such a variable in R? I don't necessarily need it to be created within the matrix itself either.
Many thanks in advance.