[R] update matrix with subset of it where only row names match
Martin Waller
martinej.waller at ntlworld.com
Thu Nov 15 21:04:47 CET 2007
jim holtman wrote:
> Lets take a look at your solution:
>
>> mat1 <- matrix(0, nrow=10, ncol=3)
>> dimnames(mat1) <- list(paste('row', 1:10, sep=''), LETTERS[1:3])
>> mat2 <- matrix(1:3, ncol=1, dimnames=list(c('row3', 'row7', 'row5'), "B"))
>> mat2
> B
> row3 1
> row7 2
> row5 3
>> mat1[rownames(mat2)%in%rownames(mat1),"B"]=mat2[,"B"]
> Error in mat1[rownames(mat2) %in% rownames(mat1), "B"] = mat2[, "B"] :
> number of items to replace is not a multiple of replacement length
>> rownames(mat2)%in%rownames(mat1)
> [1] TRUE TRUE TRUE
>> mat2[,"B"]
> row3 row7 row5
> 1 2 3
>
> I got an error statement using your statement with %in%. This is
> because it produces a vector a 3 TRUE values are you can see above.
> With recycling to will the matrix, you get the error message. What
> you want to provide is the index value of the rows to replace in.
> What you would need in this case is the following statement:
>
> mat1[match(rownames(mat2), rownames(mat1)),"B"]=mat2[,"B"]
>
> Now your solution would have to be changed everytime you wanted a
> different column replaced. My solution determined which of the column
> names matched in the objects.
>
> In R, there are a number of ways of doing things. As to which is
> 'better', it all depends. In most cases it is probably a matter of
> 'style' or what a person is used to. "Better" does come into play
> when you are taking about performance and there might be a factor of
> 10X, 100X or 1000X depending on how you used some statements. I
> happen to like to try to break things down into some simple steps so
> if I have to go back later, I think I might be able to understand it
> again.
>
> If you are coming from a C/Java background, then one of hard things to
> get your mind around it to think in terms of 'vectorized' operations
> and also the difference in some of the ways that you create/manipulate
> data structures in R vs. some other languages.
>
> HTH
>
Thankyou for you explanation and time - very helpful.
Best wishes,
Martin
More information about the R-help
mailing list