replace a value in a matrix using index
2
0
Entering edit mode
Mark B ▴ 20
@mark-b-5525
Last seen 9.6 years ago
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? Thanks, MAB [[alternative HTML version deleted]]
GO GO • 7.8k views
ADD COMMENT
0
Entering edit mode
@sean-davis-490
Last seen 12 weeks ago
United States
On Mon, Oct 1, 2012 at 8:17 PM, Mark B <mablfm at="" gmail.com=""> 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") mat[5580,4]=NA Note that matrices can contain only one type, so you will not be able to put in a character value without your whole matrix being coerced to character (probably not what you want). Hope that helps. Sean > 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? > 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
ADD COMMENT
0
Entering edit mode
@james-w-macdonald-5106
Last seen 14 hours ago
United States
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
ADD COMMENT

Login before adding your answer.

Traffic: 704 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6