Dictionary style replace multiple items

前端 未结 10 623
太阳男子
太阳男子 2020-11-22 05:02

I have a large data.frame of character data that I want to convert based on what is commonly called a dictionary in other languages.

Currently I am going about it li

10条回答
  •  后悔当初
    2020-11-22 05:40

    Used @Ramnath's answer above, but made it read (what to be replaced and what to be replaced with) from a file and use gsub rather than replace.

    hrw <- read.csv("hgWords.txt", header=T, stringsAsFactor=FALSE, encoding="UTF-8", sep="\t") 
    
    for (i in nrow(hrw)) 
    {
    document <- gsub(hrw$from[i], hrw$to[i], document, ignore.case=TRUE)
    }
    

    hgword.txt contains the following tab separated

    "from"  "to"
    "AA"    "0101"
    "AC"    "0102"
    "AG"    "0103" 
    

提交回复
热议问题