Hello Nikhil and Wu,
Thank you very much for your reply!
What I want is to calculate the student's score column-wise by ignoring the 
specific values such as zeros for example only using c(2,1) in column 8 in the 
tmp1 and generate tmp2. I changed the zeros to NAs and modified my stud fun to 
student as below. So I can ignore these specific values but can not put back 
the NAs to the right position in the resulted matrix.
Any suggestions to put back NAs to their original positions in column 7 and 8 
either to list tmp4 or matrix tmp5?

tmp1[tmp1==0]<-NA
student<- function(x){
    x<-x[!is.na(x)]
    x<-(x-mean(x))/sd(x)
    return (x)
}
tmp4<-apply(tmp1, 2, student)
tmp4
[[1]]
[1]  1.1296201  0.1613743 -1.2909944  1.1296201 -0.3227486 -0.8068715

[[2]]
[1]  0.0000000  0.0000000 -1.4680505 -0.5872202  0.5872202  1.4680505

[[3]]
[1] -0.5207910 -0.4817316 -0.4036130  0.2994548  1.9008870 -0.7942062

[[4]]
[1] -0.8630035 -0.2380699 -0.7737273 -0.5951748  1.5474546  0.9225210

[[5]]
[1] -0.1913482 -0.6944435 -0.7202433  0.5826446  1.7565336 -0.7331431

[[6]]
[1] -0.1132277  0.5661385 -1.4719601 -0.7925939  0.5661385  1.2455047

[[7]]
[1] -0.9561829  0.2390457  1.4342743  0.2390457 -0.9561829

[[8]]
[1]  0.7071068 -0.7071068

tmp5<- matrix(unlist(tmp4),nrow=6)
tmp5
           [,1]       [,2]       [,3]       [,4]       [,5]       [,6]       
[,7]       [,8]
[1,]  1.1296201  0.0000000 -0.5207910 -0.8630035 -0.1913482 -0.1132277 
-0.9561829 -0.7071068
[2,]  0.1613743  0.0000000 -0.4817316 -0.2380699 -0.6944435  0.5661385  
0.2390457  1.1296201
[3,] -1.2909944 -1.4680505 -0.4036130 -0.7737273 -0.7202433 -1.4719601  
1.4342743  0.1613743
[4,]  1.1296201 -0.5872202  0.2994548 -0.5951748  0.5826446 -0.7925939  
0.2390457 -1.2909944
[5,] -0.3227486  0.5872202  1.9008870  1.5474546  1.7565336  0.5661385 
-0.9561829  1.1296201
[6,] -0.8068715  1.4680505 -0.7942062  0.9225210 -0.7331431  1.2455047  
0.7071068 -0.3227486

Joshua

----- Original Message -----
From: "Shuhua Zhan" <sz...@uoguelph.ca>
To: r-help@r-project.org
Sent: Thursday, July 15, 2010 11:08:34 PM GMT -05:00 US/Canada Eastern
Subject: [R] how to skip a specific value when using apply() function to a 
matrix?

Hello R experts,
I'd like to studentize a matrix (tmp1) by column using apply() function and 
skip some specific values such as zeros in the example below to tmp2 but not 
tmp3. I used the script below and only can get a matrix tmp3. Could you please 
help me to studentize the matrix (tmp1) without changing the zeros and generate 
a new matrix tmp2?
Thanks,
Joshua

tmp1
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]   15    7    9   49   60    3    2    0
[2,]   13    7   10   56   21    4    3    0
[3,]   10    2   12   50   19    1    0    2
[4,]   15    5   30   52  120    2    4    0
[5,]   12    9   71   76  211    4    3    1
[6,]   11   12    2   69   18    5    2    0

tmp2
[1,]  1.1296201  0.0000000 -0.5207910 -0.8630035 -0.1913482 -0.1132277 
-0.9561829  0.0000000
[2,]  0.1613743  0.0000000 -0.4817316 -0.2380699 -0.6944435  0.5661385  
0.2390457  0.0000000
[3,] -1.2909944 -1.4680505 -0.4036130 -0.7737273 -0.7202433 -1.4719601  
0.0000000  0.7071068
[4,]  1.1296201 -0.5872202  0.2994548 -0.5951748  0.5826446 -0.7925939  
1.4342743  0.0000000
[5,] -0.3227486  0.5872202  1.9008870  1.5474546  1.7565336  0.5661385  
0.2390457 -0.7071068
[6,] -0.8068715  1.4680505 -0.7942062  0.9225210 -0.7331431  1.2455047 
-0.9561829  0.0000000

tmp3
          [,1]       [,2]       [,3]       [,4]       [,5]       [,6]      [,7] 
      [,8]
[1,]  1.1296201  0.0000000 -0.5207910 -0.8630035 -0.1913482 -0.1132277 
-0.243975 -0.5976143
[2,]  0.1613743  0.0000000 -0.4817316 -0.2380699 -0.6944435  0.5661385  
0.487950 -0.5976143
[3,] -1.2909944 -1.4680505 -0.4036130 -0.7737273 -0.7202433 -1.4719601 
-1.707825  1.7928429
[4,]  1.1296201 -0.5872202  0.2994548 -0.5951748  0.5826446 -0.7925939  
1.219875 -0.5976143
[5,] -0.3227486  0.5872202  1.9008870  1.5474546  1.7565336  0.5661385  
0.487950  0.5976143
[6,] -0.8068715  1.4680505 -0.7942062  0.9225210 -0.7331431  1.2455047 
-0.243975 -0.5976143

Here is my script:
stud<- function(x){
    x<-(x-mean(x))/sd(x)
    return (x)
}
tmp3<-apply(tmp1,2,stud)

______________________________________________
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.

______________________________________________
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