我的字符串中包含州名。我如何有效地缩写它们?我知道state.abb[grep("New York", state.name)]
但这只有在“纽约”是整个字符串时才有用。例如,我有“纽约的沃尔玛”。提前谢谢!
我们假设这个输入:
x = c("Walmart, New York", "Hobby Lobby (California)", "Sold in Sears in Illinois")
编辑:所需的输出将是“沃尔玛,纽约”,“爱好大厅(CA)”,“在伊利诺伊州的西尔斯出售”。从这里可以看到,状态可以在字符串中以多种方式出现
答案 0 :(得分:5)
以下是使用gregexpr()
,regmatches()
和regmatches<-()
的基本R方式:
abbreviateStateNames <- function(x) {
pat <- paste(state.name, collapse="|")
m <- gregexpr(pat, x)
ff <- function(x) state.abb[match(x, state.name)]
regmatches(x, m) <- lapply(regmatches(x, m), ff)
x
}
x <- c("Hobby Lobby (California)",
"Hello New York City, here I come (from Greensboro North Carolina)!")
abbreviateStateNames(x)
# [1] "Hobby Lobby (CA)"
# [2] "Hello NY City, here I come (from Greensboro NC)!"
或者 - 更自然地 - 您可以使用 gsubfn 包完成同样的事情:
library(gsubfn)
pat <- paste(state.name, collapse="|")
gsubfn(pat, function(x) state.abb[match(x, state.name)], x)
[1] "Hobby Lobby (CA)"
[2] "Hello NY City, here I come (from Greensboro NC)!"