简体   繁体   中英

Selecting and then replacing an element of a matrix in R

Extremely beginner, and the whole concept of doing things randomly in R is something I haven't managed to grasp yet.

My code below is the beginnings of me trying to create a matrix that is made up of a random distribution of the letters a - e (with replacement etc) which is my vector "bob.a". What I then need to do is select a random element from this matrix, and replace it with another sample of "bob.a" (so another random letter, even if that letter ends the same). Ideally I'd then run this through a loop something like 100 times until every element has randomly been replaced (though that every element is replaced is not important over the grand scheme of just making sure I randomly replace elements 100 times).

My code is:

##Create Bob Vector
bob.a<-factor(c(letters[1:5]))
##Check Bob Vector
bob.a


#Matrix creation (Random)
bob.matrix<- matrix(sample(bob.a,25,replace=TRUE),
                      nrow=5,
                      ncol=5,
                      byrow = TRUE)
##Check bobmatrix.v2
bob.matrix


##Random removal (&replace?)
bob.matrix[2,3] <- sample(bob.a,1,replace=TRUE)

sample(bob.matrix,1)

bob.matrix[,] <- sample(bob.a,1,replace=TRUE)

sample(bob.matrix,1) <- sample(bob.a,1,replace=TRUE)

(I'm doing everything very basic and checking every step because I'm in serious need of understanding the core concepts of what I'm doing!)

While I'm sure the answer will involve sample and maybe index, just doing so randomly is totally lost on me right now.

I tried just doing a simple replacement, just to check I knew how to, but that didn't even work because the line

bobmatrix.v2[2,3] <- sample(bob.a,1,replace=TRUE)

Inserts a number value for the text (eg replaces position [2,3] with a 1 instead of an a) which has totally thrown me off. The last two lines of code are me trying different things that don't work for what I thought of.

In the random replacement I thought it may use either indexing or sampling but my knowledge of indexing is quite low. And what I've seen online often seems to be based around replacing certain elements (such as all of the "a"'s with something else) or some variation of that, instead of selecting a single random element within the matrix, in the same way that sample does (but being able to replace that element).

Ultimately I need 3 (what I thought were simple) things:

  1. Select a random element/position in the matrix
  2. Select a random letter from a vector "sample(bob.a,1)
  3. Replace 1 with 2 (random element/position with random letter from vector.)

Any help would be appreciated, and while it seems relatively pointless it's a core concept for some programming I'm going to have to be doing over the summer and I really, really want to be able to do and understand what is happening to be sure I can actually do the project I've committed to.

Also, I've not looked into the loop part of this yet, so any guidance there would be great.

Many Thanks

First: you want a character matrix, so don't use factor s. Just define:

bob.a<-letters[1:5]

or convert it through bob.a<-as.character(bob.a) if it already exists. bob.matrix is ok as it is created. Just don't use byrow=TRUE : which is its purpose, since the element are random? If you want to replace a random element of bob.matrix just try:

bob.matrix[sample(length(bob.matrix),1)]<-sample(bob.a,1)

OK, here is a simple and hopefully self-explanatory code:

sample.letters <- factor(c(letters[1:5]))
sample.matrix <- matrix(sample(sample.letters,25,replace=TRUE), nrow=5, ncol=5, byrow = TRUE)

random.row <- sample(1:nrow(sample.matrix),1)
random.col <- sample(1:ncol(sample.matrix),1)

sample.matrix.2 <- sample.matrix
sample.matrix.2[random.row, random.col] <- as.character(sample(sample.letters,1))

#before
sample.matrix
# what's changed?
paste("changed: ",random.row,",",random.col)
#after
sample.matrix.2

which produces

> #before
> sample.matrix
     [,1] [,2] [,3] [,4] [,5]
[1,] "c"  "d"  "b"  "b"  "e" 
[2,] "a"  "b"  "d"  "a"  "b" 
[3,] "c"  "d"  "d"  "a"  "c" 
[4,] "c"  "d"  "e"  "d"  "b" 
[5,] "a"  "d"  "d"  "a"  "c" 
> # what's changed?
> paste("changed: ",random.row,",",random.col)
[1] "changed:  1 , 3"
> #after
> sample.matrix.2
     [,1] [,2] [,3] [,4] [,5]
[1,] "c"  "d"  "a"  "b"  "e" 
[2,] "a"  "b"  "d"  "a"  "b" 
[3,] "c"  "d"  "d"  "a"  "c" 
[4,] "c"  "d"  "e"  "d"  "b" 
[5,] "a"  "d"  "d"  "a"  "c" 

You problem with the assignment is the fact that you're using factor , so following:

sample(sample.letters,1)

returns the factor as int , so if you really just want the factor as character (ie letter) you need to coerce it:

as.character(sample(sample.letters,1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM