Q:

Looping arithmetic calculation between tables

I have a table that look like this:
table1

            Not Visible Visible <NA>
All           0.29       0.50   0.20
Bowtie        0.24       0.17   0.59
Cola          0.15       0.83   0.02
Squig         0.49       0.51   0.49

I then have 9 other similar tables. Below is an example:
 table2

          Not Visible Visible <NA>
All           0.28    0.50    0.23
Bowtie        0.11    0.30    0.59
Cola          0.30    0.67    0.03
Squig         0.42    0.51    0.06

I want the result of table1 - table2 as below but I also want table 1 with each of the other 9 tables.
       Not Visible  Visible  <NA>
All           0.01    0.00 -0.03
Bowtie        0.13   -0.13  0.00
Cola         -0.15    0.16 -0.01
Squig         0.07    0.00  0.43

How do I do this without writing Table 1 - table 2; table 1 - table 3; table 1 - table 4 etc?
If I try looping with the code below (as an example), I get the non-numeric argument to binary error:
Tables <- c("table1", "table2") ## as an example

for (r in Tables) {

yy <- paste(r,"res", sep = "-")
  zz <- table1-r
  assign(yy,zz)

}

Any ideas?

A:

Consider using a list of tables (not the string literals of their names) and then use lapply() where resulting list can be saved as individual tables or binded into dataframe:
# LIST OF TABLES WITH NAMED ELEMENTS (t1 NOT INCLUDED)
tables <- setNames(list(t2, t3, t4, t5, t6, t7, t8, t9), 
                   c("table2", "table3", "table4", "table5",
                     "table6", "table7", "table8", "table9"))

# ITERATIVELY SUBTRACT FROM t1
tableList <- lapply(tables, function(x) t1 - x)

# SAVE EACH TABLE AS SEPARATE OBJECTS 
list2env(tableList, envir=.GlobalEnv)

# DATAFRAME BINDING - WIDE FORMAT (INCLUDING t1)
df <- as.data.frame(cbind(t1, do.call(cbind, tableList)))

# DATAFRAME BINDING - LONG FORMAT (INCLUDING t1)
df <- as.data.frame(rbind(t1, do.call(rbind, tableList)))

