[R] Help: calculations based on three matrices
Berend Hasselman
bhh at xs4all.nl
Tue Sep 24 17:15:31 CEST 2013
On 24-09-2013, at 16:51, arun <smartpink111 at yahoo.com> wrote:
> Hi,
> Try:
> ME<- matrix(NA,dim(geno[1]),dim(geno)[2])
> indx0<- geno==0 & !is.na(geno)
> ME[indx0]<- Mx[indx0]+My[indx0]
> indx1<- geno==1 & !is.na(geno)
> ME[indx1]<- apply(cbind(Mx[indx1],My[indx1]),1,max)
>
I would do this to avoid the apply
ME[indx1]<- pmax(Mx[indx1],My[indx1])
Berend
> ME
> A.K.
>
>
>
>
> ----- Original Message -----
> From: JiangZhengyu <zhyjiang2006 at hotmail.com>
> To: "r-help at r-project.org" <r-help at r-project.org>
> Cc:
> Sent: Tuesday, September 24, 2013 10:07 AM
> Subject: [R] Help: calculations based on three matrices
>
>
>
>
>
>
>
>
>
> 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
>
>
>
>
> ______________________________________________
> 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.
>
>
> ______________________________________________
> 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