[R] Replace value in a matrix according to a list with a list of value .

arun smartpink111 at yahoo.com
Thu Jan 9 16:59:27 CET 2014


Hi,
You may also try:
vec1 <- (seq(nrow(mm))-1)*ncol(mm)
pos2 <- unlist(mapply("+",pos,vec1))
mm1 <- t(mm)
mm1[pos2] <- unlist(gty)
identical(res,t(mm1))
#[1] TRUE

A.K.






On Thursday, January 9, 2014 9:40 AM, arun <smartpink111 at yahoo.com> wrote:
Hi,
Try:

res <-  do.call(rbind,lapply(seq_len(nrow(mm)),function(i) {x1 <- mm[i,]; x1[pos[[i]]] <- gty[[i]]; x1}))

A.K.




On Thursday, January 9, 2014 9:28 AM, Mohammad Tanvir Ahamed <mashranga at yahoo.com> wrote:
Hi there !!

I have a matrix like 
> mm
      [,1] [,2] [,3]
 [1,]    1   11   21
 [2,]    2   12   22
 [3,]    3   13   23

I have a list of position index like 
> pos

$row1
[1] 1 3

$row2
[1] 3 2

$row3
[1] 1 3 2

I have a list of values like
> gty

$v1

 9  3 


$v2

 4  8 


$v3

 7  4  1 

 
Now i want to replace the value of each row in mm by pos list with value gty

> mm
      [,1] [,2] [,3]
 [1,]    9   11   3
 [2,]    8   12   4
 [3,]    7   1   4


Any suggestion regarding this problem will be very helpful .
Thank you. 

Best regards


........................... 
Tanvir Ahamed
Göteborg, Sweden
    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list