I am new to R and am looking to merge two or more frequency tables into one. I have searched around but have been unable to find exactly what I need. I have two frequency tables obtained from two sample texts t0<-table(strsplit(tolower("It was the age of wisdom it was the age of foolishness it was the epoch of belief"), "\\W")) t1<-table(strsplit(tolower("it was the epoch of incredulity it was the season of Light it was the season of Darkness"), "\\W")) so I get:> t0age belief epoch foolishness it of the was wisdom 2 1 1 1 3 3 3 3 1> t1darkness epoch incredulity it light of season the was 1 1 1 3 1 3 2 3 3 I need to merge these two tables into one so that the frequencies for each word are added, e.g. the word "it" would have 6. So resulting table would be age belief darkness epoch foolishness incredulity it light 2 1 1 2 1 1 6 1 of season the was wisdom 6 2 6 6 1 Any suggestions? -- View this message in context: http://r.789695.n4.nabble.com/Merging-two-or-more-frequency-tables-tp4643663.html Sent from the R help mailing list archive at Nabble.com.
HI, Try this: dt0<-data.frame(t0) ?dt1<-data.frame(t1) res1<-merge(dt0,dt1,by="Var1") res2<-aggregate(Freq.x+Freq.y~Var1,data=res1,sum) colnames(res2)[2]<-"Freq" res3<-rbind( dt0[!dt0$Var1%in%res2$Var1,],dt1[!dt1$Var1%in%res2$Var1,] ,res2) row.names(res3)<-1:nrow(res3) ?xtabs(Freq~Var1,data=res3) #Var1 ? # ???? age????? belief?????? epoch foolishness????????? it????????? of ??? # ???? 2?????????? 1?????????? 2?????????? 1?????????? 6?????????? 6 ????? #? the???????? was????? wisdom??? darkness incredulity?????? light ??????? #? 6?????????? 6?????????? 1?????????? 1?????????? 1?????????? 1 ??? # season ????? # ?? 2 ----- Original Message ----- From: mcelis <mcelis at lightminersystems.com> To: r-help at r-project.org Cc: Sent: Wednesday, September 19, 2012 3:08 PM Subject: [R] Merging two or more frequency tables I am new to R and am looking to merge two or more frequency tables into one. I have searched around but have been unable to find exactly what I need. I have two frequency tables obtained from two sample texts t0<-table(strsplit(tolower("It was the age of wisdom it was the age of foolishness it was the epoch of belief"), "\\W")) t1<-table(strsplit(tolower("it was the epoch of incredulity it was the season of Light it was the season of Darkness"), "\\W")) so I get:> t0? ? ? ? age? ? ? belief? ? ? epoch? foolishness? ? ? it? ? ? ? ? of? ? ? ? the? ? ? ? was? wisdom ? ? ? ? ? ? 2? ? ? ? ? ? ? 1? ? ? ? ? ? ? ? ? 1? ? ? ? ? ? ? ? ? ? ? 1? ? ? 3? ? ? ? ? 3? ? ? ? ? ? 3? ? ? ? ? ? ? 3? ? ? ? ? ? ? ? 1> t1? darkness? ? ? epoch? incredulity? ? ? ? it? ? ? light? ? ? ? ? of? ? season? ? ? ? the? was ? ? ? ? ? ? ? ? ? 1? ? ? ? ? ? ? ? 1? ? ? ? ? ? ? ? ? ? 1? ? ? ? ? 3? ? ? ? ? 1? ? ? ? ? ? 3? ? ? ? ? ? ? ? 2? ? ? ? ? ? ? 3? ? ? ? 3 I need to merge these two tables into one so that the frequencies for each word are added, e.g. the word "it" would have 6. So resulting table would be ? ? ? age? ? ? belief? ? darkness? ? ? epoch? ? foolishness? ? incredulity? ? ? ? ? it? ? ? light ? ? ? ? ? 2? ? ? ? ? ? ? 1? ? ? ? ? ? ? ? ? ? 1? ? ? ? ? ? ? ? 2? ? ? ? ? ? ? ? ? ? ? ? 1? ? ? ? ? ? ? ? ? ? ? 1? ? ? ? ? 6? ? ? ? ? ? 1 ? ? ? ? of? ? ? season? ? ? ? ? ? the? ? ? ? ? ? was? ? ? ? ? wisdom ? ? ? ? ? 6? ? ? ? ? ? ? ? 2? ? ? ? ? ? ? ? 6? ? ? ? ? ? ? ? ? 6? ? ? ? ? ? ? ? ? ? ? 1 Any suggestions? -- View this message in context: http://r.789695.n4.nabble.com/Merging-two-or-more-frequency-tables-tp4643663.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hello, Try the following. fun <- function(x, ...){ z <- Reduce(paste, list(x, ...)) tsum <- table(strsplit(tolower(z), "\\W")) tsum } x <- "It was the age of wisdom it was the age of foolishness it was the epoch of belief" y <- "it was the epoch of incredulity it was the season of Light it was the season of Darkness" fun(x, y) z <- "It was the era of R" fun(x, y, z) Hope this helps, Rui Barradas Em 19-09-2012 20:08, mcelis escreveu:> I am new to R and am looking to merge two or more frequency tables into one. > I have searched around but have been unable to find exactly what I need. > > I have two frequency tables obtained from two sample texts > > t0<-table(strsplit(tolower("It was the age of wisdom it was the age of > foolishness it was the epoch of belief"), "\\W")) > t1<-table(strsplit(tolower("it was the epoch of incredulity it was the > season of Light it was the season of Darkness"), "\\W")) > > so I get: >> t0 > age belief epoch foolishness it of > the was wisdom > 2 1 1 1 > 3 3 3 3 1 >> t1 > darkness epoch incredulity it light of > season the was > 1 1 1 3 > 1 3 2 3 3 > > I need to merge these two tables into one so that the frequencies for each > word are added, e.g. the word "it" > would have 6. So resulting table would be > > age belief darkness epoch foolishness > incredulity it light > 2 1 1 2 > 1 1 6 1 > of season the was wisdom > 6 2 6 6 > 1 > > Any suggestions? > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Merging-two-or-more-frequency-tables-tp4643663.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Both solutions work great. Thank you for your help. -- View this message in context: http://r.789695.n4.nabble.com/Merging-two-or-more-frequency-tables-tp4643663p4643692.html Sent from the R help mailing list archive at Nabble.com.