且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

提取R中每个单词的第一个字母

更新时间:2022-11-16 16:54:48

您可以使用 gsub 删除所有不需要的字符并保留您想要的字符.从预期的输出来看,您似乎仍在使用 3 个字符长的单词中的字符:

You can use gsub to delete all the unwanted characters and remain with the ones you want. From the expected output, it seems you are still using characters from words tht are 3 characters long:

 gsub('\\b(\\pL)\\pL{2,}|.','\\U\\1',sentences,perl = TRUE)
[1] "DPCS"   "DSOPGR" "DASP"   "DAI"  

但如果我们忽略你指出的词,那就是:

But if we were to ignore the words you indicated then it would be:

gsub('\\b(\\pL)\\pL{4,}|.','\\U\\1',sentences,perl = TRUE)
[1] "DPCS" "DOGR" "DSP"  "DAI"