Merging frequency distributions

> addFreq <- function(P,Q){
+    if(length(Q)==0) return(P)
+    H <- c(); sent <- "zzzzzzzzzzz" 
+    N <- c(names(P),sent); M <- c(names(Q),sent); 
+    n <- length(P); m <- length(Q); i <- 1; j <- 1 
+    while((i<n)|(j<m)){
+       if(N[i]==M[j]) {H[N[i]] <- P[i]+Q[j]; i <- i+1; j <- j+1}
+       else if (N[i]<M[j]) {H[N[i]] <- P[i]; i <- i+1}
+       else {H[M[j]] <- Q[j]; j <- j+1}
+    }
+    return(H)
+ }

> t1 <- "Analytics in a Big Data World: The Essential Guide to Data Science and Its Applications"
> t2 <- "Data Science for Business: What You Need to Know about Data Mining and Data-Analytic Thinking"
> separator <- "([[:punct:]]|[[:space:]]|”|“|—|‘|’)+"
> f1 <- table(unlist(strsplit(tolower(t1),separator)))
> f1

           a    analytics          and applications          big         data    essential 
           1            1            1            1            1            2            1 
       guide           in          its      science          the           to        world 
           1            1            1            1            1            1            1 
> f2 <- table(unlist(strsplit(tolower(t2),separator)))
> f2

   about analytic      and business     data      for     know   mining     need  science 
       1        1        1        1        3        1        1        1        1        1 
thinking       to     what      you 
       1        1        1        1 
> f12 <- addFreq(f1,f2)
> f12
           a        about     analytic    analytics          and applications          big 
           1            1            1            1            2            1            1 
    business         data    essential          for        guide           in          its 
           1            5            1            1            1            1            1 
        know       mining         need      science          the     thinking           to 
           1            1            1            2            1            1            2 
        what 
           1 
ru/hse/eda21/stu/merge.txt · Last modified: 2022/01/13 04:02 by vlado
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki