我有这样的东西:
#Change deg
x1 <- gsub("°","°",x1)
#Change ohm
x1 <- gsub("Ω","Ω",x1)
#change divide
x1 <- gsub("÷","÷",x1)
#change minus
x1 <- gsub("−","-",x1)
但是有很多值,我不想将所有内容都写在 r 脚本中。我有一张 table ,看起来像这样
&newtonm; N m
® ®
° °
− -
如何查找表格并在 R 脚本中进行更改?
最佳答案
以下是如何使用 Reduce()
来完成此操作:
lookup <- data.frame(pat=c('&newtonm;','®','°','−'),rep=c('N m','®','°','-'),stringsAsFactors=F);
lookup;
## pat rep
## 1 &newtonm; N m
## 2 ® ®
## 3 ° °
## 4 − -
entityrep <- function(x) Reduce(function(x,r) gsub(lookup$pat[r],lookup$rep[r],x,fixed=T),seq_len(nrow(lookup)),x);
x1 <- 'test &newtonm; ® ° − test';
entityrep(x1);
## [1] "test N m ® ° - test"
关于使用 R 中的查找表替换字符向量中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31828218/