Guest User

Untitled

a guest
Jan 15th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. To revise the funciton to delete the global variable in R function?
  2. word <- c('abc noboby@stat.berkeley.edu','text with no email','first me@mything.com also you@yourspace.com')
  3. pattern <- '[-A-Za-z0-9_.%]+@[-A-Za-z0-9_.%]+\.[A-Za-z]+'
  4.  
  5.  
  6. getmail<-function(pattern,word){
  7. mail<<-c()
  8. sapply(word,function(x){
  9. out<-gregexpr(pattern,x)
  10. for (i in 1:length(out[[1]])){
  11. if (out[[1]][i]>0)
  12. mail<<-union(mail,substr(x,start=out[[1]][i],stop=out[[1]][i]+attr(out[[1]],"match.length")[i]-1))
  13. }})
  14. return(mail)
  15. }
  16.  
  17. getmail(pattern,word)
  18.  
  19. [1] "noboby@stat.berkeley.edu" "me@mything.com" "you@yourspace.com"
  20. ls()
  21. [1] "getmail" "mail" "pattern" "word"
  22.  
  23. mail<-c()
  24. out<-gregexpr(pattern,word)
  25. for (i in 1:length(out)){
  26. for (j in 1:length(out[[i]])){
  27. if (out[[i]][j]>0)
  28. mail<-union(mail,substr(word[i],start=out[[i]][j],stop=out[[i]][j]+attr(out[[i]],"match.length")[j]-1))}}
  29. mail
  30. [1] "noboby@stat.berkeley.edu" "me@mything.com" "you@yourspace.com"
  31.  
  32. > m <- gregexpr(pattern,word)
  33. > lapply(seq_along(word),
  34. function(i){substring(word[i],m[[i]],m[[i]] + attr(m[[i]],"match.length"))})
  35. [[1]]
  36. [1] "noboby@stat.berkeley.edu"
  37.  
  38. [[2]]
  39. [1] ""
  40.  
  41. [[3]]
  42. [1] "me@mything.com " "you@yourspace.com"
Add Comment
Please, Sign In to add comment