我有一个矩阵。矩阵中的单个数据条目是字符串。例如,“藏红花素片”。该矩阵包含许多以“平板电脑”结尾的条目。对于矩阵中的每个条目,我想用“tab”替换“tablet”这个词。我怎么能在 R 中做到这一点?
问问题
598 次
1 回答
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"
于 2015-12-22T21:16:00.357 回答