r - Looping arithmetic calculation between tables -
i have table this:
table1 not visible visible <na> 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 have 9 other similar tables. below example:
table2 not visible visible <na> 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 result of table1 - table2
below want table 1 each of other 9 tables.
not visible visible <na> 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 without writing table 1 - table 2; table 1 - table 3; table 1 - table 4
etc? if try looping code below (as example), non-numeric argument binary error:
tables <- c("table1", "table2") ## example (r in tables) { yy <- paste(r,"res", sep = "-") zz <- table1-r assign(yy,zz) }
any ideas?
consider using list of tables (not string literals of names) , use lapply()
resulting list can saved individual tables or binded dataframe:
# list of tables 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 t1 tablelist <- lapply(tables, function(x) t1 - x) # save each table 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)))
Comments
Post a Comment