从单词[R]制作首字母缩略词

时间:2016-06-16 13:59:44

标签: r

我试图将多个单词转换为首字母缩略词,如下例所示: 例如对于text1,我们可以拥有" pil cwf lso ..."。

但基本上我有两个问题:我不知道sapply中的多个gsub是不起作用的,只有第一个(这里的gsub是" lso")

我尝试了一个名为" a"但是gsub并不喜欢矢量...

感谢

text1 <- "  iforce credit workflow loan studio Demande sur une validation     finale  CWF  Demande sur une validation finale"
text2 <- "  LSO  Pb lors loan-studio du pricing Opportunity ID  O1079224 \nPb somme repayment different du facility amount "
text3 <- "  CWF Abandon d une loanstudio autorisation avec un ceiling CWF Abandon d une autorisation avec un ceiling authosheet\nRequest ID  2015_10_003639"

df <- data.frame(x = c(text1, text2, text3)) 

df<- sapply(df,
  function (data) {
a <- c("loan studio", "loan-studio", "loanstudio")
    gsub(a, "lso", x = data)  
  gsub("credit workflow", "cwf", x = data)  

    gsub("pilotage", "pil", x = data)                                    
    gsub("iforce", "pil", x = data)                                    
    gsub("i-force", "pil", x = data)   
    gsub("force", "pil", x = data)                                    
    gsub("iforce", "pil", x = data) 

      }
)

df

更新:所以新代码是:我试图通过添加数据&lt; - str_replace_all(a,&#34; lso&#34;,数据)来存储结果,但它没有&#39;解决了。​​

text1 <- "  iforce credit workflow loan studio Demande sur une validation    finale  credit workflow  Demande sur une validation finale"
text2 <- "  LSO  Pb lors loan-studio du pricing Opportunity ID  O1079224 \nPb somme repayment different du facility amount "
text3 <- "  CWF Abandon d une loanstudio autorisation avec un ceiling CWF Abandon d une autorisation avec un ceiling authosheet\nRequest ID  2015_10_003639"

library(stringr) # permet d'utiliser str_replace_all

df <- data.frame(x = c(text1, text2, text3)) 

df2 <- df

 df<- sapply(df,
     function (data) {
    a <- c("loan studio", "loan-studio", "loanstudio")
     str_replace_all(a, "lso", data)  

    str_replace_all("credit workflow", "cwf", data)  

    b <- c("pilotage", "iforce", "i-force", "force", "iforce")
    str_replace_all(b, "pil", data)                                             
      }
)

df2
df

0 个答案:

没有答案