Here is a faster way of doing the replacement: (provide reproducible
data next time)

> x <- matrix(sample(6:9, 64, TRUE), 8)
> x
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    8    7    7    6    7    8    7    9
[2,]    7    7    8    6    7    6    7    7
[3,]    7    7    7    6    9    6    6    7
[4,]    9    9    7    6    8    7    6    6
[5,]    6    9    9    8    8    9    8    9
[6,]    9    7    6    9    7    8    6    7
[7,]    7    9    8    9    7    9    7    8
[8,]    9    9    6    9    9    8    8    6
> x.f <- 1:8  # replacement values based on column
> x.ind <- which(x == 9, arr.ind=TRUE)
> x.ind
      row col
 [1,]   4   1
 [2,]   6   1
 [3,]   8   1
 [4,]   4   2
 [5,]   5   2
 [6,]   7   2
 [7,]   8   2
 [8,]   5   3
 [9,]   6   4
[10,]   7   4
[11,]   8   4
[12,]   3   5
[13,]   8   5
[14,]   5   6
[15,]   7   6
[16,]   1   8
[17,]   5   8
> x[x.ind] <- x.f[x.ind[,'col']]
> x
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    8    7    7    6    7    8    7    8
[2,]    7    7    8    6    7    6    7    7
[3,]    7    7    7    6    5    6    6    7
[4,]    1    2    7    6    8    7    6    6
[5,]    6    2    3    8    8    6    8    8
[6,]    1    7    6    4    7    8    6    7
[7,]    7    2    8    4    7    6    7    8
[8,]    1    2    6    4    5    8    8    6


On Wed, Oct 28, 2009 at 12:55 PM, Marcio Resende
<mresende...@yahoo.com.br> wrote:
>
> #Mdarts is a matrix 2343x788
> #frequencia is a vector 2343x1
> # 9 in Mdarts[fri,frj] stands for my missing values which i want to replace
> by the value in the vector frequencia
>
>
> Mdarts<-t(matrix(scan("C:/GWS/CNB/dartg.txt"),ncol=nindT,nrow=nm, byrow=T))
> frequencia <- matrix(scan("C:/GWS/CNB/freq.txt"),ncol=1)
> for (fri in 1:nindT){
> for (frj in 1:nm){
> Mdarts[fri,frj] <- if (Mdarts[fri,frj] == 9) frequencia[frj] else
> Mdarts[fri,frj]
> Mdarts[fri,frj] <- Mdarts[fri,frj]/1-(frequencia[frj]^2)
> }
> }
>
> Is there a faster way to it?
> Maybe using any apply function?
> Thanks in advance
> --
> View this message in context: 
> http://www.nabble.com/Is-there-a-faster-way-to-do-it--tp26098223p26098223.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
R-help@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.

Reply via email to