How to merge tables in R?

merge, r

Solution

This will work if you want to use the variables which are present in both `a` and `b`:

n <- intersect(names(a), names(b))
a[n] + b[n]

#  3 3.3 3.5 3.6 3.7 3.8 3.9   4 4.1 4.2 4.4 
# 27   8   8   5   4   7   5   6   4   5   5

If you want to use all variables:

n <- intersect(names(a), names(b)) 

res <- c(a[!(names(a) %in% n)], b[!(names(b) %in% n)], a[n] + b[n])

res[order(names(res))] # sort the results

Problem

I think this will have a simple answer, but I can't work it out! Here is an example using the `iris` dataset: ``` a <- table(iris[,2]) b <- table(iris[,3]) ``` How do I add these two tables together? For example, the variable 3 would have a value of 27 (26+1) and variable 3.3 a value of 8 (6+2) in the new output table. Any help much appreciated.

Original source