[R] Help: calculations based on three matrices

JiangZhengyu zhyjiang2006 at hotmail.com
Tue Sep 24 16:07:33 CEST 2013


 






Dear R experts,
 
I have 3 matrices - "Mx", "My" and "geno" that have the same exact dimensions (attached).  I want to calculate a expression matrix (ME) of the same dimension as well. It is a little complicated.
 
To calculate and for a specific cell in ME,  e.g. ME[2,2],
 
 if value of geno[2,2] is 0, ME[2,2]=Mx[2,2]+My[2,2];
 if value of geno[2,2] is 1, ME[2,2]=the larger number of Mx[2,2] and My[2,2]
 if value of geno[2,2] is NA, ME[2,2]=NA
 
I tried to make codes below, but did not work and got stuck. I was wondering if anyone could help with better coding.
 
Thanks in advance,
Zhengyu
 
-----------------------------------------------   
 
Mx <-  read.table("Mx.txt",header=TRUE, sep="\t", na.strings="NA", dec=".", strip.white=TRUE)
My <-  read.table("My.txt",header=TRUE, sep="\t", na.strings="NA", dec=".", strip.white=TRUE)
geno <-  read.table("geno.txt",header=TRUE, sep="\t", na.strings="NA", dec=".", strip.white=TRUE)
 
#Calculate Exp for geno==0
ind=which(geno==0,arr.ind=T)
het=Mx+My
ME=matrix(het,nrow=nrow(geno),ncol=ncol(geno))
ME[ind]=het[ind]
 
#Calculate Exp for geno==1
ind=which(geno==1,arr.ind=T)
hom=apply(M1,c(1:2),function(x)max(M1[],M2[]))# error
ME=matrix(hom,nrow=nrow(geno),ncol=ncol(geno))
ME[ind]=hom[ind]
 
# I think the first calculation (geno==0) will be replaced but I don't know how to keep them.
 
> head(Mx)
   X1cX X2cX  X3cX  X4cX X5cX X6cX X7cX X8cX
1 26383 6252 14319 16421 2225  480 2038 1164
2   636  310   351   341  218  656  412  267
3   301  299   752   236  239  309  283  195
4  1016 1046  1364   782  822  274  437  346
5  1261 1272  1076  1037  659  337 1143 1195
6   609  414   393   459  215  429  566  236

> dim(Mx)
[1] 20  8

> head(My)
   X1cY X2cY X3cY X4cY X5cY X6cY X7cY X8cY
1  1039  233  408  607  275 1837 1319  149
2  7146 1706 2248 2346 1596  544 2696  417
3   266  425 1186  214  325  369  377  215
4   260  282  238  240  264  265  317  203
5  1070 1052  746  613  384  447  456  683
6 13032 1708 2328 5998 2529  923 1068  145

> dim(My)
[1] 20  8

> head(geno)
  P1 P2 P3 P4 P5 P6 P7 P8
1 NA NA NA NA  1 NA NA NA
2  1  1  1  1  1  1  1  1
3  1  1  0  0  0  0  1  0
4  1  1  1  1  1  1  0  0
5  0  0  0  0  0  0  1  0
6 NA NA NA NA NA NA NA NA

> dim(geno)
[1] 20  8



 		 	   		   		 	   		  
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Mx.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130924/ea519894/attachment.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: My.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130924/ea519894/attachment-0001.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: geno.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130924/ea519894/attachment-0002.txt>


More information about the R-help mailing list