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