[BioC] replace a value in a matrix using index

James W. MacDonald jmacdon at uw.edu
Tue Oct 2 16:00:13 CEST 2012


Hi Mark,

This question doesn't have anything to do with Bioconductor packages, 
and should be posed on R-help. In future, please restrict to 
BioC-related topics.

On 10/1/2012 8:17 PM, Mark B wrote:
> Hi,
> I am trying to replace a single numeric value in a matrix with a character
> vector.
>
>> mat[5580, 4]
> [1] 838.1
>
>> mat.r<- replace(mat, mat[5580, 4], "NA")
> I don't get any errors. However when I go to see if the number at mat[5580,
> 4] was replaced with "NA"
> the old number is still there. I don't get any errors.
>
>> mat.r[5580, 4]
> [1] 838.1
>
> Any hints on how to do this?

I believe this is covered in an Introduction to R. In addition, the help 
for replace says that the first argument should be a vector, so there is 
no reason to think this will work the way you have used it. That said, 
there are approximately a bazillion ways to do what you want. Three that 
readily come to mind are:

 > mat <- matrix(rnorm(100), 10)
 > is.na(mat[,1]) <- 3
 > mat[4,5] <- NA
 > mat[,2] <- replace(mat[,2], 5, NA)

Note that you don't want to put an "NA" in a numeric matrix, as this 
will coerce the matrix to character. Instead you want an NA.

Best,

Jim

> Thanks,
>
> MAB
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at r-project.org
> https://stat.ethz.ch/mailman/listinfo/bioconductor
> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor

-- 
James W. MacDonald, M.S.
Biostatistician
University of Washington
Environmental and Occupational Health Sciences
4225 Roosevelt Way NE, # 100
Seattle WA 98105-6099



More information about the Bioconductor mailing list