R中字符串向量的自定义缩写

时间:2017-08-09 15:04:09

标签: r string

我尝试从数据框degree_abrev中提取的向量gss中对某些字符串进行自定义缩写。

这是我能想到的......但是我想知道是否有人有更漂亮的"方式...

degree_abrev <- gsub("Lt High School", "LtHS", gss$degree)
degree_abrev <- gsub("High School", "HS", degree_abrev)
degree_abrev <- gsub("Junior College", "JC", degree_abrev)
degree_abrev <- gsub("Bachelor", "B", degree_abrev)
degree_abrev <- gsub("Graduate", "G", degree_abrev)

2 个答案:

答案 0 :(得分:1)

&#34; plyr&#34;包有&#34; mapvalues&#34;这样做的功能。我相信必须有其他方法来做到这一点。

> degree_abbrev <- c("Lt High School", "High School", "Junior College", 
"Bachelor", "Graduate")

> degree_abbrev
[1] "Lt High School" "High School"    "Junior College" "Bachelor"       
"Graduate"      

> degree_abbrev <- mapvalues(degree_abbrev, from = c("Lt High School", "High 
School", "Junior College", "Bachelor", "Graduate"), to = c("LtHS", "HS", 
"JC", "B", "G"))

> degree_abbrev
[1] "LtHS" "HS"   "JC"   "B"    "G"

答案 1 :(得分:0)

我不知道这是否更漂亮,但我更喜欢使用sapply。

degree_abrev <- c("Lt High School", "High School", "Junior College", "Bachelor", "Graduate")

sapply(strsplit(degree_abrev, " "), function(x){paste(substring(x, 1, 1), collapse = "")})
[1] "LHS" "HS"  "JC"  "B"   "G"