替换R中矩阵内的单词

时间:2013-09-04 06:41:09

标签: r text matrix

我有一个矩阵。矩阵中的单个数据条目是字符串。例如,“crocin tablet”。矩阵包含许多条目,最后带有“平板电脑”。对于矩阵中的每个条目,我想用“tab”替换“tablet”这个词。我怎么能在R?中做到这一点?

1 个答案:

答案 0 :(得分:0)

让Ananda Mahto的解决方案更加明确。

> 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"