删除首字母缩写词中的点

Removing dots in acronyms

我有一个带有首字母缩略词的向量,例如 "U.S."

我想删除字符之间的点,但我不想删除整个文档中的所有点,所以只删除首字母缩写词中的点。

我可以使用 gsub:

text <- c("U.S.", "U.N.", "C.I.A")
gsub("U.S.", "US", text)

但是我如何告诉 R 删除所有可能的首字母缩略词中的所有点(即也在 "U.N." 或 "C.I.A." 中)?

你可以在这里分界

gsub('\b\.','',vec)

或在评论中说明更简单的选项!

您的问题似乎与您提供的代码有点不同:您想替换可能包含 acronyms/abbreviations.[= 点的文本中的首字母缩略词13=]

此代码通过搜索重复的大写点组合来提取和识别首字母缩略词(可以在工作流程中手动检查和过滤这些组合以确保它没有发现任何奇怪的东西),然后使用 mgsub 来自 Replace multiple arguments with gsub

的代码
text1 <- c("The U.S. and the C.I.A. are acronyms. They should be matched.")
m <- gregexpr("([A-Z]\.)+", text1)
matches <- regmatches(text1, m)[[1]]
matches_nodot <- sapply(matches, gsub, pattern = "\.", replacement = "")

mgsub <- function(pattern, replacement, x, ...) {
  if (length(pattern)!=length(replacement)) {
    stop("pattern and replacement do not have the same length.")
  }
  result <- x
  for (i in 1:length(pattern)) {
    result <- gsub(pattern[i], replacement[i], result, ...)
  }
  result
}


text2 <- mgsub(matches, matches_nodot, text1)
text2
# [1] "The US and the CIA are acronyms. They should be matched."