简体   繁体   中英

Replacing words within a matrix in R

I have a matrix. A single data entry in the matrix is a character string. For example, "crocin tablet". The matrix contains many entries with "tablet" at the end. I want to replace the word "tablet" with "tab" for every entry within the matrix. How can I do that in R?

Just making Ananda Mahto's solution more explicit.

> newMatrix <- matrix(data=c("Abbott Laboratories tablet",
+                            "AbbVie tablet",
+                            "Acadia Pharmaceuticals tablet",
+                            "Acorda Therapeutics tablet",
+                            "Actavis tablet",
+                            "Actelion tablet",
+                            "Advanced Chemical Industries tablet",
+                            "Advaxis tablet",
+                            "Ajanta Pharma tablet",
+                            "Alcon tablet"), nrow=5, ncol = 2)
> gsub("tablet", "tab", newMatrix)
     [,1]                         [,2]                              
[1,] "Abbott Laboratories tab"    "Actelion tab"                    
[2,] "AbbVie tab"                 "Advanced Chemical Industries tab"
[3,] "Acadia Pharmaceuticals tab" "Advaxis tab"                     
[4,] "Acorda Therapeutics tab"    "Ajanta Pharma tab"               
[5,] "Actavis tab"                "Alcon tab"  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM