简体   繁体   中英

Convert a function into an apply, sapply (data.frame)

So I just built this function which basically takes two strings (a text, and a set of keywords). Then it has to find how many keywords are contained by the text string, if any. I been trying to apply the code on a data frame with no success.

Function is working:

something=function(text,keywords){
  kw = unlist(strsplit(keywords, ","))
  c=0
  for(i in length(kw)){
    if(grepl(kw[i],text)==0){
      c=c+1
    } else {c}
  }
  return(c)
}

Where if I imput:

> something("this planetarium is the shit","planetarium,amazing")
[1] 1

But what if my data frame was df

     keyword         text_clean
1    planetarium     Man this planetarium is the shit
2 musee,africain     rt lyonmangels reste encore places franceangels tour lyon organisons  investissons pme

My output expected is:

  df.1
1  1
2  0

Any insight? I was trying this code:

substng<-function(text, keywords){

  vector = laply(text,function(text,keywords){
    kw = unlist(strsplit(keywords, ","))
    c=0
    for(i in length(kw)){
      if(grepl(kw[i],text)==0){
        c=c+1
      } else {c}
    }
    return(c)
  })
  vector.df= as.data.frame(vector)
}

df <- read.table(header = TRUE, stringsAsFactors = FALSE, text = "keyword         text_clean
planetarium     'Man this planetarium is the shit'
musee,africain     'rt lyonmangels reste encore places franceangels tour lyon organisons  investissons pme'")

df$count = substng(df$text_clean,df$keyword)

I think stri_count in the stringi package can accomplish this.

Use "pattern|amazing" as the pattern/regex. Pipe = "or".

https://cran.r-project.org/web/packages/stringi/stringi.pdf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM