问:
Using a different variable in each iteration of a function
Hi I've got a function  which performs calculations on several columns (written with kind support form stack overflow community). I'd like to adapt it so that instead of calculating MOD for each iteration, it looks up a value in a table and uses that. The MOD for each column will be different.

Here is the function that does the calculations:

```<code>library(data.table)
dataDT<- data.frame(CAG = c(13, 14, 15, 17),
                A01 = c(6485,35,132, 12),
                A02 = c(0,42,56, 4))

thres <- 0.2
dataDT<-setDT(dataDT)

colsToBeUsed<-names(dataDT[,!'CAG'])
sumDataSetdata<-c()
sumDataSet<-unlist(lapply(X=1:length(colsToBeUsed),function(X){s=colsToBeUsed[X]
  eval(parse(text=paste0('dataDT[',s,'<thres*max(',s,'),',s,':=0]')))
  eval(parse(text=paste0('dataDT[,MOD',s,':=dataDT[',s,'==max(',s,'),CAG]]')))
  eval(parse(text=paste0('dataDT[,norm',s,':=',s,'/sum(',s,')]')))
  eval(parse(text=paste0('dataDT[,sum',s,':=',s,'/sum(',s,')*(CAG-MOD',s,'),]')))
  eval(parse(text=paste0('rbind(sumDataSetdata,dataDT[,sum(sum',s,')])')))
  }))
</code>```

Here is the table which gives the MOD:

```<code>MODs <- data.frame(c(data.frame(samples = c('A01', 'A02', 'A03', 'A04'), MOD = c(117.8, 120.2, 124.5, 130.6))
</code>```

Here is the table which says which MOD to use for each 'sample' column

```<code>ctrls <- (data.frame(samples = c('A01', 'A02', 'A03', 'A04'), ctrl = c(A01, A01, A03, A03))
</code>```

Response to answer 1

Thank you, that works well for the example. I've been trying to apply it to my real data and am having a few difficulties. Here is the code for my real data.

```<code>library(data.table)
dataDT <- data.frame(area[,7:ncol(height)])

dataDT <- setDT(dataDT)

colsToBeUsed<-names(dataDT[,!'CAG'])

MODs <- data.frame(samples = samples$unique.inputdf.SampleFileName., MOD = htresults$mode)

ctrls <- data.frame(samples = samples$unique.inputdf.SampleFileName., ctrl = 'A01_RR20170609_FA_A01_2017-06-09_1.fsa')

myFun <- function(x, mod, cag, thres) {
  x[x < (thres * max(x))] <- 0
  norm_x <- x / sum(x)
  sum_x <- norm_x * (cag - mod)
  sum(sum_x)
}

transision_matrix <- merge(ctrls, MODs, by.x = "ctrl", by.y = "samples")
setDT(transision_matrix)

mf2 <- function(colname, dataDT, transision_matrix){
  x <- dataDT[, colname, with = F][[1]]
  mod <- transision_matrix[samples == colname, MOD]
  cag <- dataDT[, "CAG"][[1]]
  myFun(x, mod, cag, thres = 0.2)
}

sapply(colsToBeUsed, function(x) mf2(x, dataDT, transision_matrix))
</code>```

The control sample for all columns in this experiment is A01_RR20170609_FA_A01_2017.06.09_1.fsa and its mode is 20.67000

This is the result I get

```<code>A01_RR20170609_FA_A01_2017.06.09_1.fsa A02_RR20170609_FA_A02_2017.06.09_1.fsa
                                     0                                      0
A03_RR20170609_FA_A03_2017.06.09_1.fsa A04_RR20170609_FA_A04_2017.06.09_1.fsa
                                     0                                      0
A05_RR20170609_FA_A05_2017.06.09_1.fsa A06_RR20170609_FA_A06_2017.06.09_1.fsa
                                     0                                      0
A07_RR20170609_FA_A07_2017.06.09_1.fsa A08_RR20170609_FA_A08_2017.06.09_1.fsa
                                     0                                      0
A09_RR20170609_FA_A09_2017.06.09_1.fsa A10_RR20170609_FA_A10_2017.06.09_1.fsa
                                     0                                      0
A11_RR20170609_FA_A11_2017.06.09_1.fsa A12_RR20170609_FA_A12_2017.06.09_1.fsa
                                 0                                      0
</code>```

The results I'm expecting are:

```<code> [1]  4.108246  5.868355  4.608756 -1.159657  4.015066  4.364199  5.262355  4.337760  6.496672  5.574396
[11]  5.102111  8.911440
</code>```

In case it's useful, here is info about the table 'height':

```<code>'data.frame':   660 obs. of  19 variables:
 $ Dye/SamplePeak                        : chr  "B,66" "B,67" "B,68" "B,69" ...
 $ Marker                                : chr  NA NA NA NA ...
 $ Allele                                : chr  NA NA NA NA ...
 $ Size                                  : num  144 147 148 150 151 ...
 $ Area                                  : num  148288 110 907 3355 1274 ...
 $ DataPoint                             : num  2591 2622 2641 2655 2671 ...
 $ CAG                                   : num  13.9 14.9 15.5 15.9 16.4 ...
 $ A01_RR20170609_FA_A01_2017-06-09_1.fsa: num  6485 32 125 450 211 ...
 $ A02_RR20170609_FA_A02_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A03_RR20170609_FA_A03_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A04_RR20170609_FA_A04_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A05_RR20170609_FA_A05_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A06_RR20170609_FA_A06_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A07_RR20170609_FA_A07_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A08_RR20170609_FA_A08_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A09_RR20170609_FA_A09_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A10_RR20170609_FA_A10_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A11_RR20170609_FA_A11_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A12_RR20170609_FA_A12_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
</code>```

The table 'height' is quite large, but I've added the results from column 'CAG' and 'A01_RR20170609_FA_A01_2017.06.09_1.fsa' in case it's useful for working.

```<code>**CAG**
  [1] 13.92667 14.88000 15.46333 15.89333 16.38333 16.84333 17.39667 17.79667 18.41000 18.78000 19.39333
 [12] 19.76000 20.33667 20.67000 21.03333 21.36667 23.52000 24.45667 25.39667 27.31000 28.34667 29.32000
 [23] 30.23333 31.17667 32.15000 32.72667 33.08333 33.67667 34.03667 34.60667 34.96667 35.54000 35.93333
 [34] 36.54000 36.87333 37.49333 37.87333 38.50667 38.89000 39.48333 39.84667 40.45000 40.81000 41.41000
 [45] 41.73667 42.45333 42.77667 43.34000 43.69333 46.21000 49.21333 52.21667 52.60667 13.90000 14.95000
 [56] 15.47333 15.99667 16.46000 16.86333 17.48000 17.85000 18.43667 18.80667 19.42667 19.76333 20.43667
 [67] 21.38667 21.72333 22.70000 23.31333 23.61667 24.23000 24.59667 25.60333 26.55333 27.16333 27.44000
 [78] 28.42000 29.40000 30.37667 30.98667 31.32333 32.30000 33.23667 34.16667 35.13333 36.07667 37.05667
 [89] 38.03333 38.63333 39.01667 39.60667 39.97333 40.57667 40.94000 41.54333 41.87333 42.47333 42.83333
[100] 43.46000 43.78667 44.70333 12.48333 13.03667 13.92667 14.88000 15.49333 15.86333 16.44667 16.78333
[111] 17.36667 17.82667 18.38000 18.84333 19.42333 19.79000 20.34000 20.79667 21.28333 21.74333 23.60000
[122] 24.54000 25.51333 26.48667 27.43333 28.40667 29.32000 29.96000 30.26333 30.90333 31.36000 32.24000
[133] 33.17333 34.25667 35.40667 36.01667 36.56667 36.99667 37.58667 37.97000 38.60333 38.98333 39.57667
[144] 39.94000 40.54667 40.90667 41.51000 41.86667 42.43667 42.79333 43.38667 43.74000 12.63667 13.95667
[155] 15.52333 15.92333 16.53667 16.90667 17.49000 17.89000 18.44333 18.81000 19.42333 19.79000 20.40000
[166] 20.76667 21.37667 21.74333 22.38000 24.54000 25.51333 27.46333 28.43667 29.04667 29.38000 30.29333
[177] 31.26667 32.24000 33.17333 34.10667 35.04333 35.68000 35.98667 36.59667 36.96333 37.55667 37.97000
[188] 38.60333 38.95333 39.57667 39.94000 40.51333 40.87333 41.47333 41.83333 42.43000 42.75667 43.35000
[199] 43.70667 44.29667 44.65000 45.24000 45.56000 46.54333 47.54333 12.45333 13.04000 13.87333 14.86667
[210] 15.48667 15.86000 16.44667 16.79000 17.37667 17.81333 18.43333 18.80667 19.42667 19.79667 20.41333
[221] 20.75000 21.36667 21.73333 22.59667 23.54667 24.53000 25.51000 26.46000 27.44000 28.38667 29.00000
[232] 29.33667 29.91667 30.28333 31.26333 31.84333 33.08667 34.08000 34.68667 35.08333 35.97333 36.37333
[243] 36.96000 37.59000 37.94667 38.59000 38.98000 39.57667 39.94333 40.55000 40.88333 41.48667 41.85000
[254] 42.42000 42.77667 43.37333 43.73000 44.29333 44.64667 45.26667 45.56000 12.57667 12.94000 13.55333
[265] 13.92000 14.50000 14.86667 15.41667 15.87667 16.51667 16.88667 17.49667 17.86333 18.47667 18.81333
[276] 19.42333 19.79000 20.40000 20.76667 21.40667 21.74333 23.60000 27.42667 28.94333 29.76333 30.33667
[287] 32.21333 33.17000 34.09333 34.57333 35.05667 35.69000 36.05333 36.69000 36.99667 37.58667 37.97000
[298] 38.60333 38.98333 39.60333 39.96333 40.56333 40.92000 41.48667 41.84333 42.43333 42.78667 43.40667
[309] 43.70000 12.05333 12.51333 12.91333 13.90000 14.48667 14.85667 15.47667 15.78333 16.40000 16.83333
[320] 17.48000 17.88333 18.47000 18.81000 19.39333 19.79000 20.40000 20.76667 21.34667 21.71000 22.26000
[331] 22.62333 23.56667 24.57333 26.43000 27.99333 28.66667 29.40000 31.29333 32.30000 33.17000 34.09000
[342] 34.69000 35.08000 35.65333 36.02000 36.23000 36.93333 37.52667 37.94667 38.59000 38.94667 39.57667
[353] 39.94000 40.54333 40.90333 41.47333 41.83333 42.42667 42.78333 43.40333 43.72667 52.24333 52.57667
[364] 12.02667 12.30000 13.73667 14.99333 15.57333 15.94000 16.52333 16.92000 17.44000 17.96000 18.44667
[375] 18.81333 19.45333 19.81667 20.42000 20.78333 21.35667 21.66000 22.41333 24.52333 25.52000 26.45333
[386] 27.42667 28.06333 28.33333 28.94000 29.36333 29.91000 30.30333 30.88000 31.24333 31.85000 32.18333
[397] 33.14333 33.77000 34.13000 34.70000 35.03000 35.66333 36.02667 36.63333 37.00000 37.58333 37.99000
[408] 38.58333 38.96000 39.57333 39.93333 40.53000 40.88667 41.48000 41.83667 42.43000 42.78333 43.37000
[419] 43.69333 44.36667 44.69000 45.27333 45.65000 46.24000 46.54000 50.86667 51.52667 12.42333 13.03333
[430] 13.85667 14.47000 14.86667 15.47667 15.84667 16.39667 16.70000 17.40667 17.80333 19.21000 19.79000
[441] 20.18667 20.73667 21.31667 21.71000 22.20000 22.59333 23.56667 24.51000 25.48333 26.42667 27.03333
[452] 27.33667 28.33667 29.30667 29.94333 30.91333 31.91000 32.78667 33.64667 34.00333 34.63333 34.99333
[463] 35.59667 35.99333 36.54000 36.93667 37.52333 37.90333 38.54000 38.92000 39.54667 39.90667 40.51000
[474] 40.84000 41.44000 41.79667 42.39333 42.74667 43.31000 43.69333 44.22000 52.21667 52.60667 12.27000
[485] 13.79333 14.95000 15.44000 16.01667 16.44333 16.84000 17.47667 17.84333 18.45000 18.81667 19.42333
[496] 19.69667 20.12000 20.48000 21.38667 21.68667 22.59333 23.52667 24.52333 25.12667 25.45667 26.45667
[507] 26.97333 27.39667 28.33667 28.97333 29.36667 29.91333 30.27667 31.24667 31.82000 32.18333 33.14000
[518] 34.06000 34.65667 35.01667 35.61333 35.97333 36.54667 36.94000 37.52000 37.92667 38.55333 38.92667
[529] 39.51333 39.87000 40.46667 40.82000 41.41333 41.76667 42.35667 42.71000 43.29667 43.64667 44.23000
[540] 44.58000 45.21667 45.53667 46.15000 46.51000 54.90333 12.13000 12.51667 13.81333 14.84000 15.44333
[551] 15.89333 16.74000 17.37333 17.76333 18.33667 18.73000 19.30333 19.60333 20.37667 21.30333 21.63000
[562] 22.52333 23.50667 24.48667 25.46667 26.38667 26.92333 27.34000 27.90667 28.29333 29.24333 30.22667
[573] 30.79333 31.24000 31.83333 32.43000 32.93000 33.46000 34.02333 34.67333 34.97000 35.53667 35.92333
[584] 36.52333 36.88333 37.48667 37.85667 38.50333 38.87333 39.45333 39.83667 40.42667 40.81000 41.36667
[595] 41.71667 42.33000 42.68000 43.26333 43.61000 44.56333 48.30000 12.46000 12.84667 13.83333 14.43000
[606] 14.82000 15.35667 15.65667 16.40333 17.42000 17.78000 18.55667 18.88333 19.30333 19.69000 20.31333
[617] 20.64000 21.23333 21.61667 22.21000 22.47667 23.45667 24.04667 24.40333 24.99333 25.35000 25.94000
[628] 26.29667 26.92000 27.27667 27.84000 28.25667 28.85000 29.20333 30.15333 31.13000 31.72000 32.10667
[639] 32.69667 33.04333 33.91667 34.91000 35.91000 36.44333 36.79667 37.39333 37.79333 38.41000 38.78000
[650] 39.39333 39.77333 40.36000 40.71000 41.29000 41.64000 42.24667 42.59333 43.54667 44.49000 45.40333
</code>```

A01_RR20170609_FA_A01_2017.06.09_1.fsa

```<code>  [1]  6485    32   125   450   211   703   553  1549  1360  3526  5028 13610 15986 31233   713  1260
 [17]    31    37    33    46    43    48    40    63    78    33   118    40   176    65   296   103
 [33]   501   242   923   545  2006  1355  4348  2564  8615  3886 12985   227   669    85    61    57
 [49]   103    32    42    50    64     0     0     0     0     0     0     0     0     0     0     0
 [65]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 [81]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 [97]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[113]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[129]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[145]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[161]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[177]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[193]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[209]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[225]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[241]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[257]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[273]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[289]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[305]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[321]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[337]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[353]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[369]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[385]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[401]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[417]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[433]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[449]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[465]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[481]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[497]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[513]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[529]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[545]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[561]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[577]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[593]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[609]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[625]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[641]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[657]     0     0     0     0
</code>```

Response to answer 2
dputs as requested

```<code>dput(transision_matrix)
structure(list(ctrl = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = "A01_RR20170609_FA_A01_2017-06-09_1.fsa", class = "factor"),
    samples = structure(1:12, .Label = c("A01_RR20170609_FA_A01_2017-06-09_1.fsa",
"A02_RR20170609_FA_A02_2017-06-09_1.fsa", "A03_RR20170609_FA_A03_2017-06-09_1.fsa",
"A04_RR20170609_FA_A04_2017-06-09_1.fsa", "A05_RR20170609_FA_A05_2017-06-09_1.fsa",
"A06_RR20170609_FA_A06_2017-06-09_1.fsa", "A07_RR20170609_FA_A07_2017-06-09_1.fsa",
"A08_RR20170609_FA_A08_2017-06-09_1.fsa", "A09_RR20170609_FA_A09_2017-06-09_1.fsa",
"A10_RR20170609_FA_A10_2017-06-09_1.fsa", "A11_RR20170609_FA_A11_2017-06-09_1.fsa",
"A12_RR20170609_FA_A12_2017-06-09_1.fsa"), class = "factor"),
MOD = c(20.67, 20.67, 20.67, 20.67, 20.67, 20.67, 20.67,
20.67, 20.67, 20.67, 20.67, 20.67)), .Names = c("ctrl", "samples",
"MOD"), row.names = c(NA, -12L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x10180d178>, index = structure(integer(0), "`__samples`" = integer(0)))
> dput(ctrls)
structure(list(samples = structure(1:12, .Label =     c("A01_RR20170609_FA_A01_2017-06-09_1.fsa",
"A02_RR20170609_FA_A02_2017-06-09_1.fsa", "A03_RR20170609_FA_A03_2017-06-09_1.fsa",
"A04_RR20170609_FA_A04_2017-06-09_1.fsa", "A05_RR20170609_FA_A05_2017-06-09_1.fsa",
"A06_RR20170609_FA_A06_2017-06-09_1.fsa", "A07_RR20170609_FA_A07_2017-06-09_1.fsa",
"A08_RR20170609_FA_A08_2017-06-09_1.fsa", "A09_RR20170609_FA_A09_2017-06-09_1.fsa",
"A10_RR20170609_FA_A10_2017-06-09_1.fsa", "A11_RR20170609_FA_A11_2017-06-09_1.fsa",
    "A12_RR20170609_FA_A12_2017-06-09_1.fsa"), class = "factor"),
    ctrl = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L), class = "factor", .Label = "A01_RR20170609_FA_A01_2017-06-09_1.fsa")), .Names = c("samples",
    "ctrl"), row.names = c(NA, -12L), class = "data.frame")
    > dput(MODs)
    structure(list(samples = structure(1:12, .Label = c("A01_RR20170609_FA_A01_2017-06-09_1.fsa",
    "A02_RR20170609_FA_A02_2017-06-09_1.fsa", "A03_RR20170609_FA_A03_2017-06-09_1.fsa",
"A04_RR20170609_FA_A04_2017-06-09_1.fsa", "A05_RR20170609_FA_A05_2017-06-09_1.fsa",
"A06_RR20170609_FA_A06_2017-06-09_1.fsa", "A07_RR20170609_FA_A07_2017-06-09_1.fsa",
"A08_RR20170609_FA_A08_2017-06-09_1.fsa", "A09_RR20170609_FA_A09_2017-06-09_1.fsa",
"A10_RR20170609_FA_A10_2017-06-09_1.fsa", "A11_RR20170609_FA_A11_2017-06-09_1.fsa",
    "A12_RR20170609_FA_A12_2017-06-09_1.fsa"), class = "factor"),
    MOD = c(20.67, 19.7633333333333, 16.7833333333333, 21.7433333333333,
    16.79, 14.8666666666667, 15.7833333333333, 21.66, 16.3966666666667,
    19.6966666666667, 19.6033333333333, 15.3566666666667)), .Names = c("samples",
    "MOD"), row.names = c(NA, -12L), class = "data.frame")
</code>```

答:
Thanks. This is the function at the moment:

```<code>library(data.table)
dataDT <- height[,13:ncol(height)] #Create a data frame containing CAG and height columns
dataDT <- setDT(dataDT) #Convert to a data table

colsToBeUsed<-names(dataDT[,!'CAG']) #Assigns the columns to be analysed

myFun <- function(x, mod, cag, thres) { #Function that takes vectors as arguments.
  x[x < (thres * max(x))] <- 0 #First sets all heights < 0.2*threshold to 0.
  norm_x <- x / sum(x) #Then normalises heights by dividing by the sum of the heights.
  sum_x <- norm_x * (cag - mod) #Then multiplies by the change in CAG from mode
  sum(sum_x) #Then sums the results
}

transision_matrix <- merge(propsettings, modeHt, by.x = "control", by.y = "sample") #Transition matrix that determines control modes for each sample
setDT(transision_matrix)

mf2 <- function(colname, dataDT, transision_matrix){ #Function that takes column name and data table as arguments.
  x <- dataDT[, colname, with = F][[1]]
  mod <- transision_matrix[sample == colname, mode] #Vector of CONTROL modes to use
  cag <- dataDT[, "CAG"][[1]]
  thres <- resultsHt$iithreshold #THIS LIKELY SOURCE OF ERROR AS IT WORKS WITH 0.2
  myFun(x, mod, cag, thres)
}

iiHt <- sapply(colsToBeUsed, function(x) mf2(x, dataDT, transision_matrix))

resultsHt$iiHt <- iiHt
</code>```

This is 'resultsHt$iithreshold':

```<code> [1] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[29] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
</code>```

This is modeHt:
                                 sample      mode
    1  A01_MF20170623_FA_A01_2017-06-23_1.fsa 130.93667
    2  A02_MF20170623_FA_A02_2017-06-23_1.fsa 131.14667
    3  A03_MF20170623_FA_A03_2017-06-23_1.fsa 132.07333
    4  A05_MF20170623_FA_A05_2017-06-23_1.fsa 151.97333
    5  A06_MF20170623_FA_A06_2017-06-23_1.fsa 128.39333
    6  A07_MF20170623_FA_A07_2017-06-23_1.fsa 116.02333
    7  A08_MF20170623_FA_A08_2017-06-23_1.fsa 127.42667
    8  A09_MF20170623_FA_A09_2017-06-23_1.fsa 163.22000
    9  A10_MF20170623_FA_A10_2017-06-23_1.fsa 131.92667
    10 A11_MF20170623_FA_A11_2017-06-23_1.fsa 133.57333
    11 A12_MF20170623_FA_A12_2017-06-23_1.fsa 164.85333
    12 B01_MF20170623_FA_B01_2017-06-23_1.fsa 180.34333
    13 B02_MF20170623_FA_B02_2017-06-23_1.fsa 133.08000
    14 B03_MF20170623_FA_B03_2017-06-23_1.fsa 163.53333
    15 B04_MF20170623_FA_B04_2017-06-23_1.fsa 133.13333
    16 B05_MF20170623_FA_B05_2017-06-23_1.fsa 133.08000
    17 B06_MF20170623_FA_B06_2017-06-23_1.fsa 167.23000
    18 B07_MF20170623_FA_B07_2017-06-23_1.fsa 115.05667
    19 B08_MF20170623_FA_B08_2017-06-23_1.fsa 179.62333
    20 C01_MF20170623_FA_C01_2017-06-23_1.fsa 115.93000
    21 C02_MF20170623_FA_C02_2017-06-23_1.fsa 115.17333
    22 C05_MF20170623_FA_C05_2017-06-23_1.fsa 131.18667
    23 C07_MF20170623_FA_C07_2017-06-23_1.fsa 131.13333
    24 C08_MF20170623_FA_C08_2017-06-23_1.fsa 131.13000
    25 C09_MF20170623_FA_C09_2017-06-23_1.fsa 130.09333
    26 C10_MF20170623_FA_C10_2017-06-23_1.fsa 115.09000
    27 C11_MF20170623_FA_C11_2017-06-23_1.fsa 130.04000
    28 C12_MF20170623_FA_C12_2017-06-23_1.fsa 115.70000
    29 D02_MF20170623_FA_D02_2017-06-23_1.fsa 116.03667
    30 D03_MF20170623_FA_D03_2017-06-23_1.fsa 131.14000
    31 D04_MF20170623_FA_D04_2017-06-23_1.fsa 115.22667
    32 D05_MF20170623_FA_D05_2017-06-23_1.fsa  19.88000
    33 D06_MF20170623_FA_D06_2017-06-23_1.fsa  19.91000
    34 D08_MF20170623_FA_D08_2017-06-23_1.fsa  19.84667
    35 D10_MF20170623_FA_D10_2017-06-23_1.fsa  72.32333
    36 D11_MF20170623_FA_D11_2017-06-23_1.fsa 130.00333
    37 D12_MF20170623_FA_D12_2017-06-23_1.fsa 130.01333
    38 A01_MF20170522_FA_A01_2017-05-22_1.fsa 136.94667
    39 C02_MF20170529_FA_C02_2017-05-30_1.fsa 132.31667
    40 B08_MF20170522_FA_B08_2017-05-22_1.fsa 121.00000

This is 'propsettings':

```<code>                              sample                                control
1  A01_MF20170623_FA_A01_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
2  A02_MF20170623_FA_A02_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
3  A03_MF20170623_FA_A03_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
4  A05_MF20170623_FA_A05_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
5  A06_MF20170623_FA_A06_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
6  A07_MF20170623_FA_A07_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
7  A08_MF20170623_FA_A08_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
8  A09_MF20170623_FA_A09_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
9  A10_MF20170623_FA_A10_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
10 A11_MF20170623_FA_A11_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
11 A12_MF20170623_FA_A12_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
12 B01_MF20170623_FA_B01_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
13 B02_MF20170623_FA_B02_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
14 B03_MF20170623_FA_B03_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
15 B04_MF20170623_FA_B04_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
16 B05_MF20170623_FA_B05_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
17 B06_MF20170623_FA_B06_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
18 B07_MF20170623_FA_B07_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
19 B08_MF20170623_FA_B08_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
20 C01_MF20170623_FA_C01_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
21 C02_MF20170623_FA_C02_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
22 C05_MF20170623_FA_C05_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
23 C07_MF20170623_FA_C07_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
24 C08_MF20170623_FA_C08_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
25 C09_MF20170623_FA_C09_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
26 C10_MF20170623_FA_C10_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
27 C11_MF20170623_FA_C11_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
28 C12_MF20170623_FA_C12_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
29 D02_MF20170623_FA_D02_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
30 D03_MF20170623_FA_D03_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
31 D04_MF20170623_FA_D04_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
32 D05_MF20170623_FA_D05_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
33 D06_MF20170623_FA_D06_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
34 D08_MF20170623_FA_D08_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
35 D10_MF20170623_FA_D10_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
36 D11_MF20170623_FA_D11_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
37 D12_MF20170623_FA_D12_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
38 A01_MF20170522_FA_A01_2017-05-22_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
39 C02_MF20170529_FA_C02_2017-05-30_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
40 B08_MF20170522_FA_B08_2017-05-22_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
</code>```

This is a summary of what 'height' looks like:

```<code>> str(height)
'data.frame':   1078 obs. of  53 variables:
 $ Dye/SamplePeak                        : chr  "B,65" "B,66" "B,67" "B,68" ...
 $ Marker                                : chr  NA NA NA NA ...
 $ Allele                                : chr  NA NA NA NA ...
 $ Size                                  : num  418 432 435 438 441 ...
 $ Area                                  : num  285 354 300 334 342 385 359 370 410 439 ...
 $ DataPoint                             : num  5665 5827 5859 5890 5924 ...
 $ flank                                 : num  108 108 108 108 108 108 108 108 108 108 ...
 $ correction                            : num  2 2 2 2 2 2 2 2 2 2 ...
 $ start                                 : num  100 100 100 100 100 100 100 100 100 100 ...
 $ end                                   : num  200 200 200 200 200 200 200 200 200 200 ...
 $ control                               : chr  "B08_MF20170522_FA_B08_2017-05-22_1.fsa" "B08_MF20170522_FA_B08_2017-05-22_1.fsa" "B08_MF20170522_FA_B08_2017-05-22_1.fsa" "B08_MF20170522_FA_B08_2017-05-22_1.fsa" ...
 $ iithreshold                           : num  0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 ...
 $ CAG                                   : num  105 110 111 112 113 ...
 $ A01_MF20170623_FA_A01_2017-06-23_1.fsa: num  31 32 32 33 40 37 36 41 45 38 ...
 $ A02_MF20170623_FA_A02_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A03_MF20170623_FA_A03_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A05_MF20170623_FA_A05_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A06_MF20170623_FA_A06_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A07_MF20170623_FA_A07_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A08_MF20170623_FA_A08_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A09_MF20170623_FA_A09_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A10_MF20170623_FA_A10_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A11_MF20170623_FA_A11_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A12_MF20170623_FA_A12_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B01_MF20170623_FA_B01_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B02_MF20170623_FA_B02_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B03_MF20170623_FA_B03_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B04_MF20170623_FA_B04_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B05_MF20170623_FA_B05_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B06_MF20170623_FA_B06_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B07_MF20170623_FA_B07_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B08_MF20170623_FA_B08_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C01_MF20170623_FA_C01_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C02_MF20170623_FA_C02_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C05_MF20170623_FA_C05_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C07_MF20170623_FA_C07_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C08_MF20170623_FA_C08_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C09_MF20170623_FA_C09_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C10_MF20170623_FA_C10_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C11_MF20170623_FA_C11_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C12_MF20170623_FA_C12_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D02_MF20170623_FA_D02_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D03_MF20170623_FA_D03_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D04_MF20170623_FA_D04_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D05_MF20170623_FA_D05_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D06_MF20170623_FA_D06_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D08_MF20170623_FA_D08_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D10_MF20170623_FA_D10_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D11_MF20170623_FA_D11_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D12_MF20170623_FA_D12_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A01_MF20170522_FA_A01_2017-05-22_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C02_MF20170529_FA_C02_2017-05-30_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B08_MF20170522_FA_B08_2017-05-22_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
</code>```

Body:
Hi I've got a function  which performs calculations on several columns (written with kind support form stack overflow community). I'd like to adapt it so that instead of calculating MOD for each iteration, it looks up a value in a table and uses that. The MOD for each column will be different.

Here is the function that does the calculations:

library(data.table)
dataDT<- data.frame(CAG = c(13, 14, 15, 17),
                A01 = c(6485,35,132, 12),
                A02 = c(0,42,56, 4))

thres <- 0.2
dataDT<-setDT(dataDT)

colsToBeUsed<-names(dataDT[,!'CAG'])
sumDataSetdata<-c()
sumDataSet<-unlist(lapply(X=1:length(colsToBeUsed),function(X){s=colsToBeUsed[X]
  eval(parse(text=paste0('dataDT[',s,'<thres*max(',s,'),',s,':=0]')))
  eval(parse(text=paste0('dataDT[,MOD',s,':=dataDT[',s,'==max(',s,'),CAG]]')))
  eval(parse(text=paste0('dataDT[,norm',s,':=',s,'/sum(',s,')]')))
  eval(parse(text=paste0('dataDT[,sum',s,':=',s,'/sum(',s,')*(CAG-MOD',s,'),]')))
  eval(parse(text=paste0('rbind(sumDataSetdata,dataDT[,sum(sum',s,')])')))
  }))

Here is the table which gives the MOD:

MODs <- data.frame(c(data.frame(samples = c('A01', 'A02', 'A03', 'A04'), MOD = c(117.8, 120.2, 124.5, 130.6))

Here is the table which says which MOD to use for each 'sample' column

ctrls <- (data.frame(samples = c('A01', 'A02', 'A03', 'A04'), ctrl = c(A01, A01, A03, A03))

Response to answer 1

Thank you, that works well for the example. I've been trying to apply it to my real data and am having a few difficulties. Here is the code for my real data.

library(data.table)
dataDT <- data.frame(area[,7:ncol(height)])

dataDT <- setDT(dataDT)

colsToBeUsed<-names(dataDT[,!'CAG'])

MODs <- data.frame(samples = samples$unique.inputdf.SampleFileName., MOD = htresults$mode)

ctrls <- data.frame(samples = samples$unique.inputdf.SampleFileName., ctrl = 'A01_RR20170609_FA_A01_2017-06-09_1.fsa')

myFun <- function(x, mod, cag, thres) {
  x[x < (thres * max(x))] <- 0
  norm_x <- x / sum(x)
  sum_x <- norm_x * (cag - mod)
  sum(sum_x)
}

transision_matrix <- merge(ctrls, MODs, by.x = "ctrl", by.y = "samples")
setDT(transision_matrix)

mf2 <- function(colname, dataDT, transision_matrix){
  x <- dataDT[, colname, with = F][[1]]
  mod <- transision_matrix[samples == colname, MOD]
  cag <- dataDT[, "CAG"][[1]]
  myFun(x, mod, cag, thres = 0.2)
}

sapply(colsToBeUsed, function(x) mf2(x, dataDT, transision_matrix))

The control sample for all columns in this experiment is A01_RR20170609_FA_A01_2017.06.09_1.fsa and its mode is 20.67000

This is the result I get

A01_RR20170609_FA_A01_2017.06.09_1.fsa A02_RR20170609_FA_A02_2017.06.09_1.fsa
                                     0                                      0
A03_RR20170609_FA_A03_2017.06.09_1.fsa A04_RR20170609_FA_A04_2017.06.09_1.fsa
                                     0                                      0
A05_RR20170609_FA_A05_2017.06.09_1.fsa A06_RR20170609_FA_A06_2017.06.09_1.fsa
                                     0                                      0
A07_RR20170609_FA_A07_2017.06.09_1.fsa A08_RR20170609_FA_A08_2017.06.09_1.fsa
                                     0                                      0
A09_RR20170609_FA_A09_2017.06.09_1.fsa A10_RR20170609_FA_A10_2017.06.09_1.fsa
                                     0                                      0
A11_RR20170609_FA_A11_2017.06.09_1.fsa A12_RR20170609_FA_A12_2017.06.09_1.fsa
                                 0                                      0

The results I'm expecting are:

 [1]  4.108246  5.868355  4.608756 -1.159657  4.015066  4.364199  5.262355  4.337760  6.496672  5.574396
[11]  5.102111  8.911440

In case it's useful, here is info about the table 'height':

'data.frame':   660 obs. of  19 variables:
 $ Dye/SamplePeak                        : chr  "B,66" "B,67" "B,68" "B,69" ...
 $ Marker                                : chr  NA NA NA NA ...
 $ Allele                                : chr  NA NA NA NA ...
 $ Size                                  : num  144 147 148 150 151 ...
 $ Area                                  : num  148288 110 907 3355 1274 ...
 $ DataPoint                             : num  2591 2622 2641 2655 2671 ...
 $ CAG                                   : num  13.9 14.9 15.5 15.9 16.4 ...
 $ A01_RR20170609_FA_A01_2017-06-09_1.fsa: num  6485 32 125 450 211 ...
 $ A02_RR20170609_FA_A02_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A03_RR20170609_FA_A03_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A04_RR20170609_FA_A04_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A05_RR20170609_FA_A05_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A06_RR20170609_FA_A06_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A07_RR20170609_FA_A07_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A08_RR20170609_FA_A08_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A09_RR20170609_FA_A09_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A10_RR20170609_FA_A10_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A11_RR20170609_FA_A11_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A12_RR20170609_FA_A12_2017-06-09_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...

The table 'height' is quite large, but I've added the results from column 'CAG' and 'A01_RR20170609_FA_A01_2017.06.09_1.fsa' in case it's useful for working.

**CAG**
  [1] 13.92667 14.88000 15.46333 15.89333 16.38333 16.84333 17.39667 17.79667 18.41000 18.78000 19.39333
 [12] 19.76000 20.33667 20.67000 21.03333 21.36667 23.52000 24.45667 25.39667 27.31000 28.34667 29.32000
 [23] 30.23333 31.17667 32.15000 32.72667 33.08333 33.67667 34.03667 34.60667 34.96667 35.54000 35.93333
 [34] 36.54000 36.87333 37.49333 37.87333 38.50667 38.89000 39.48333 39.84667 40.45000 40.81000 41.41000
 [45] 41.73667 42.45333 42.77667 43.34000 43.69333 46.21000 49.21333 52.21667 52.60667 13.90000 14.95000
 [56] 15.47333 15.99667 16.46000 16.86333 17.48000 17.85000 18.43667 18.80667 19.42667 19.76333 20.43667
 [67] 21.38667 21.72333 22.70000 23.31333 23.61667 24.23000 24.59667 25.60333 26.55333 27.16333 27.44000
 [78] 28.42000 29.40000 30.37667 30.98667 31.32333 32.30000 33.23667 34.16667 35.13333 36.07667 37.05667
 [89] 38.03333 38.63333 39.01667 39.60667 39.97333 40.57667 40.94000 41.54333 41.87333 42.47333 42.83333
[100] 43.46000 43.78667 44.70333 12.48333 13.03667 13.92667 14.88000 15.49333 15.86333 16.44667 16.78333
[111] 17.36667 17.82667 18.38000 18.84333 19.42333 19.79000 20.34000 20.79667 21.28333 21.74333 23.60000
[122] 24.54000 25.51333 26.48667 27.43333 28.40667 29.32000 29.96000 30.26333 30.90333 31.36000 32.24000
[133] 33.17333 34.25667 35.40667 36.01667 36.56667 36.99667 37.58667 37.97000 38.60333 38.98333 39.57667
[144] 39.94000 40.54667 40.90667 41.51000 41.86667 42.43667 42.79333 43.38667 43.74000 12.63667 13.95667
[155] 15.52333 15.92333 16.53667 16.90667 17.49000 17.89000 18.44333 18.81000 19.42333 19.79000 20.40000
[166] 20.76667 21.37667 21.74333 22.38000 24.54000 25.51333 27.46333 28.43667 29.04667 29.38000 30.29333
[177] 31.26667 32.24000 33.17333 34.10667 35.04333 35.68000 35.98667 36.59667 36.96333 37.55667 37.97000
[188] 38.60333 38.95333 39.57667 39.94000 40.51333 40.87333 41.47333 41.83333 42.43000 42.75667 43.35000
[199] 43.70667 44.29667 44.65000 45.24000 45.56000 46.54333 47.54333 12.45333 13.04000 13.87333 14.86667
[210] 15.48667 15.86000 16.44667 16.79000 17.37667 17.81333 18.43333 18.80667 19.42667 19.79667 20.41333
[221] 20.75000 21.36667 21.73333 22.59667 23.54667 24.53000 25.51000 26.46000 27.44000 28.38667 29.00000
[232] 29.33667 29.91667 30.28333 31.26333 31.84333 33.08667 34.08000 34.68667 35.08333 35.97333 36.37333
[243] 36.96000 37.59000 37.94667 38.59000 38.98000 39.57667 39.94333 40.55000 40.88333 41.48667 41.85000
[254] 42.42000 42.77667 43.37333 43.73000 44.29333 44.64667 45.26667 45.56000 12.57667 12.94000 13.55333
[265] 13.92000 14.50000 14.86667 15.41667 15.87667 16.51667 16.88667 17.49667 17.86333 18.47667 18.81333
[276] 19.42333 19.79000 20.40000 20.76667 21.40667 21.74333 23.60000 27.42667 28.94333 29.76333 30.33667
[287] 32.21333 33.17000 34.09333 34.57333 35.05667 35.69000 36.05333 36.69000 36.99667 37.58667 37.97000
[298] 38.60333 38.98333 39.60333 39.96333 40.56333 40.92000 41.48667 41.84333 42.43333 42.78667 43.40667
[309] 43.70000 12.05333 12.51333 12.91333 13.90000 14.48667 14.85667 15.47667 15.78333 16.40000 16.83333
[320] 17.48000 17.88333 18.47000 18.81000 19.39333 19.79000 20.40000 20.76667 21.34667 21.71000 22.26000
[331] 22.62333 23.56667 24.57333 26.43000 27.99333 28.66667 29.40000 31.29333 32.30000 33.17000 34.09000
[342] 34.69000 35.08000 35.65333 36.02000 36.23000 36.93333 37.52667 37.94667 38.59000 38.94667 39.57667
[353] 39.94000 40.54333 40.90333 41.47333 41.83333 42.42667 42.78333 43.40333 43.72667 52.24333 52.57667
[364] 12.02667 12.30000 13.73667 14.99333 15.57333 15.94000 16.52333 16.92000 17.44000 17.96000 18.44667
[375] 18.81333 19.45333 19.81667 20.42000 20.78333 21.35667 21.66000 22.41333 24.52333 25.52000 26.45333
[386] 27.42667 28.06333 28.33333 28.94000 29.36333 29.91000 30.30333 30.88000 31.24333 31.85000 32.18333
[397] 33.14333 33.77000 34.13000 34.70000 35.03000 35.66333 36.02667 36.63333 37.00000 37.58333 37.99000
[408] 38.58333 38.96000 39.57333 39.93333 40.53000 40.88667 41.48000 41.83667 42.43000 42.78333 43.37000
[419] 43.69333 44.36667 44.69000 45.27333 45.65000 46.24000 46.54000 50.86667 51.52667 12.42333 13.03333
[430] 13.85667 14.47000 14.86667 15.47667 15.84667 16.39667 16.70000 17.40667 17.80333 19.21000 19.79000
[441] 20.18667 20.73667 21.31667 21.71000 22.20000 22.59333 23.56667 24.51000 25.48333 26.42667 27.03333
[452] 27.33667 28.33667 29.30667 29.94333 30.91333 31.91000 32.78667 33.64667 34.00333 34.63333 34.99333
[463] 35.59667 35.99333 36.54000 36.93667 37.52333 37.90333 38.54000 38.92000 39.54667 39.90667 40.51000
[474] 40.84000 41.44000 41.79667 42.39333 42.74667 43.31000 43.69333 44.22000 52.21667 52.60667 12.27000
[485] 13.79333 14.95000 15.44000 16.01667 16.44333 16.84000 17.47667 17.84333 18.45000 18.81667 19.42333
[496] 19.69667 20.12000 20.48000 21.38667 21.68667 22.59333 23.52667 24.52333 25.12667 25.45667 26.45667
[507] 26.97333 27.39667 28.33667 28.97333 29.36667 29.91333 30.27667 31.24667 31.82000 32.18333 33.14000
[518] 34.06000 34.65667 35.01667 35.61333 35.97333 36.54667 36.94000 37.52000 37.92667 38.55333 38.92667
[529] 39.51333 39.87000 40.46667 40.82000 41.41333 41.76667 42.35667 42.71000 43.29667 43.64667 44.23000
[540] 44.58000 45.21667 45.53667 46.15000 46.51000 54.90333 12.13000 12.51667 13.81333 14.84000 15.44333
[551] 15.89333 16.74000 17.37333 17.76333 18.33667 18.73000 19.30333 19.60333 20.37667 21.30333 21.63000
[562] 22.52333 23.50667 24.48667 25.46667 26.38667 26.92333 27.34000 27.90667 28.29333 29.24333 30.22667
[573] 30.79333 31.24000 31.83333 32.43000 32.93000 33.46000 34.02333 34.67333 34.97000 35.53667 35.92333
[584] 36.52333 36.88333 37.48667 37.85667 38.50333 38.87333 39.45333 39.83667 40.42667 40.81000 41.36667
[595] 41.71667 42.33000 42.68000 43.26333 43.61000 44.56333 48.30000 12.46000 12.84667 13.83333 14.43000
[606] 14.82000 15.35667 15.65667 16.40333 17.42000 17.78000 18.55667 18.88333 19.30333 19.69000 20.31333
[617] 20.64000 21.23333 21.61667 22.21000 22.47667 23.45667 24.04667 24.40333 24.99333 25.35000 25.94000
[628] 26.29667 26.92000 27.27667 27.84000 28.25667 28.85000 29.20333 30.15333 31.13000 31.72000 32.10667
[639] 32.69667 33.04333 33.91667 34.91000 35.91000 36.44333 36.79667 37.39333 37.79333 38.41000 38.78000
[650] 39.39333 39.77333 40.36000 40.71000 41.29000 41.64000 42.24667 42.59333 43.54667 44.49000 45.40333

A01_RR20170609_FA_A01_2017.06.09_1.fsa

  [1]  6485    32   125   450   211   703   553  1549  1360  3526  5028 13610 15986 31233   713  1260
 [17]    31    37    33    46    43    48    40    63    78    33   118    40   176    65   296   103
 [33]   501   242   923   545  2006  1355  4348  2564  8615  3886 12985   227   669    85    61    57
 [49]   103    32    42    50    64     0     0     0     0     0     0     0     0     0     0     0
 [65]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 [81]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 [97]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[113]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[129]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[145]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[161]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[177]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[193]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[209]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[225]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[241]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[257]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[273]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[289]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[305]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[321]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[337]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[353]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[369]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[385]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[401]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[417]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[433]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[449]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[465]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[481]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[497]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[513]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[529]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[545]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[561]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[577]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[593]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[609]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[625]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[641]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[657]     0     0     0     0

Response to answer 2
dputs as requested

dput(transision_matrix)
structure(list(ctrl = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = "A01_RR20170609_FA_A01_2017-06-09_1.fsa", class = "factor"),
    samples = structure(1:12, .Label = c("A01_RR20170609_FA_A01_2017-06-09_1.fsa",
"A02_RR20170609_FA_A02_2017-06-09_1.fsa", "A03_RR20170609_FA_A03_2017-06-09_1.fsa",
"A04_RR20170609_FA_A04_2017-06-09_1.fsa", "A05_RR20170609_FA_A05_2017-06-09_1.fsa",
"A06_RR20170609_FA_A06_2017-06-09_1.fsa", "A07_RR20170609_FA_A07_2017-06-09_1.fsa",
"A08_RR20170609_FA_A08_2017-06-09_1.fsa", "A09_RR20170609_FA_A09_2017-06-09_1.fsa",
"A10_RR20170609_FA_A10_2017-06-09_1.fsa", "A11_RR20170609_FA_A11_2017-06-09_1.fsa",
"A12_RR20170609_FA_A12_2017-06-09_1.fsa"), class = "factor"),
MOD = c(20.67, 20.67, 20.67, 20.67, 20.67, 20.67, 20.67,
20.67, 20.67, 20.67, 20.67, 20.67)), .Names = c("ctrl", "samples",
"MOD"), row.names = c(NA, -12L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x10180d178>, index = structure(integer(0), "`__samples`" = integer(0)))
> dput(ctrls)
structure(list(samples = structure(1:12, .Label =     c("A01_RR20170609_FA_A01_2017-06-09_1.fsa",
"A02_RR20170609_FA_A02_2017-06-09_1.fsa", "A03_RR20170609_FA_A03_2017-06-09_1.fsa",
"A04_RR20170609_FA_A04_2017-06-09_1.fsa", "A05_RR20170609_FA_A05_2017-06-09_1.fsa",
"A06_RR20170609_FA_A06_2017-06-09_1.fsa", "A07_RR20170609_FA_A07_2017-06-09_1.fsa",
"A08_RR20170609_FA_A08_2017-06-09_1.fsa", "A09_RR20170609_FA_A09_2017-06-09_1.fsa",
"A10_RR20170609_FA_A10_2017-06-09_1.fsa", "A11_RR20170609_FA_A11_2017-06-09_1.fsa",
    "A12_RR20170609_FA_A12_2017-06-09_1.fsa"), class = "factor"),
    ctrl = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L), class = "factor", .Label = "A01_RR20170609_FA_A01_2017-06-09_1.fsa")), .Names = c("samples",
    "ctrl"), row.names = c(NA, -12L), class = "data.frame")
    > dput(MODs)
    structure(list(samples = structure(1:12, .Label = c("A01_RR20170609_FA_A01_2017-06-09_1.fsa",
    "A02_RR20170609_FA_A02_2017-06-09_1.fsa", "A03_RR20170609_FA_A03_2017-06-09_1.fsa",
"A04_RR20170609_FA_A04_2017-06-09_1.fsa", "A05_RR20170609_FA_A05_2017-06-09_1.fsa",
"A06_RR20170609_FA_A06_2017-06-09_1.fsa", "A07_RR20170609_FA_A07_2017-06-09_1.fsa",
"A08_RR20170609_FA_A08_2017-06-09_1.fsa", "A09_RR20170609_FA_A09_2017-06-09_1.fsa",
"A10_RR20170609_FA_A10_2017-06-09_1.fsa", "A11_RR20170609_FA_A11_2017-06-09_1.fsa",
    "A12_RR20170609_FA_A12_2017-06-09_1.fsa"), class = "factor"),
    MOD = c(20.67, 19.7633333333333, 16.7833333333333, 21.7433333333333,
    16.79, 14.8666666666667, 15.7833333333333, 21.66, 16.3966666666667,
    19.6966666666667, 19.6033333333333, 15.3566666666667)), .Names = c("samples",
    "MOD"), row.names = c(NA, -12L), class = "data.frame")

Body:
Thanks. This is the function at the moment:

library(data.table)
dataDT <- height[,13:ncol(height)] #Create a data frame containing CAG and height columns
dataDT <- setDT(dataDT) #Convert to a data table

colsToBeUsed<-names(dataDT[,!'CAG']) #Assigns the columns to be analysed

myFun <- function(x, mod, cag, thres) { #Function that takes vectors as arguments.
  x[x < (thres * max(x))] <- 0 #First sets all heights < 0.2*threshold to 0.
  norm_x <- x / sum(x) #Then normalises heights by dividing by the sum of the heights.
  sum_x <- norm_x * (cag - mod) #Then multiplies by the change in CAG from mode
  sum(sum_x) #Then sums the results
}

transision_matrix <- merge(propsettings, modeHt, by.x = "control", by.y = "sample") #Transition matrix that determines control modes for each sample
setDT(transision_matrix)

mf2 <- function(colname, dataDT, transision_matrix){ #Function that takes column name and data table as arguments.
  x <- dataDT[, colname, with = F][[1]]
  mod <- transision_matrix[sample == colname, mode] #Vector of CONTROL modes to use
  cag <- dataDT[, "CAG"][[1]]
  thres <- resultsHt$iithreshold #THIS LIKELY SOURCE OF ERROR AS IT WORKS WITH 0.2
  myFun(x, mod, cag, thres)
}

iiHt <- sapply(colsToBeUsed, function(x) mf2(x, dataDT, transision_matrix))

resultsHt$iiHt <- iiHt

This is 'resultsHt$iithreshold':

 [1] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[29] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2

This is modeHt:
                                 sample      mode
    1  A01_MF20170623_FA_A01_2017-06-23_1.fsa 130.93667
    2  A02_MF20170623_FA_A02_2017-06-23_1.fsa 131.14667
    3  A03_MF20170623_FA_A03_2017-06-23_1.fsa 132.07333
    4  A05_MF20170623_FA_A05_2017-06-23_1.fsa 151.97333
    5  A06_MF20170623_FA_A06_2017-06-23_1.fsa 128.39333
    6  A07_MF20170623_FA_A07_2017-06-23_1.fsa 116.02333
    7  A08_MF20170623_FA_A08_2017-06-23_1.fsa 127.42667
    8  A09_MF20170623_FA_A09_2017-06-23_1.fsa 163.22000
    9  A10_MF20170623_FA_A10_2017-06-23_1.fsa 131.92667
    10 A11_MF20170623_FA_A11_2017-06-23_1.fsa 133.57333
    11 A12_MF20170623_FA_A12_2017-06-23_1.fsa 164.85333
    12 B01_MF20170623_FA_B01_2017-06-23_1.fsa 180.34333
    13 B02_MF20170623_FA_B02_2017-06-23_1.fsa 133.08000
    14 B03_MF20170623_FA_B03_2017-06-23_1.fsa 163.53333
    15 B04_MF20170623_FA_B04_2017-06-23_1.fsa 133.13333
    16 B05_MF20170623_FA_B05_2017-06-23_1.fsa 133.08000
    17 B06_MF20170623_FA_B06_2017-06-23_1.fsa 167.23000
    18 B07_MF20170623_FA_B07_2017-06-23_1.fsa 115.05667
    19 B08_MF20170623_FA_B08_2017-06-23_1.fsa 179.62333
    20 C01_MF20170623_FA_C01_2017-06-23_1.fsa 115.93000
    21 C02_MF20170623_FA_C02_2017-06-23_1.fsa 115.17333
    22 C05_MF20170623_FA_C05_2017-06-23_1.fsa 131.18667
    23 C07_MF20170623_FA_C07_2017-06-23_1.fsa 131.13333
    24 C08_MF20170623_FA_C08_2017-06-23_1.fsa 131.13000
    25 C09_MF20170623_FA_C09_2017-06-23_1.fsa 130.09333
    26 C10_MF20170623_FA_C10_2017-06-23_1.fsa 115.09000
    27 C11_MF20170623_FA_C11_2017-06-23_1.fsa 130.04000
    28 C12_MF20170623_FA_C12_2017-06-23_1.fsa 115.70000
    29 D02_MF20170623_FA_D02_2017-06-23_1.fsa 116.03667
    30 D03_MF20170623_FA_D03_2017-06-23_1.fsa 131.14000
    31 D04_MF20170623_FA_D04_2017-06-23_1.fsa 115.22667
    32 D05_MF20170623_FA_D05_2017-06-23_1.fsa  19.88000
    33 D06_MF20170623_FA_D06_2017-06-23_1.fsa  19.91000
    34 D08_MF20170623_FA_D08_2017-06-23_1.fsa  19.84667
    35 D10_MF20170623_FA_D10_2017-06-23_1.fsa  72.32333
    36 D11_MF20170623_FA_D11_2017-06-23_1.fsa 130.00333
    37 D12_MF20170623_FA_D12_2017-06-23_1.fsa 130.01333
    38 A01_MF20170522_FA_A01_2017-05-22_1.fsa 136.94667
    39 C02_MF20170529_FA_C02_2017-05-30_1.fsa 132.31667
    40 B08_MF20170522_FA_B08_2017-05-22_1.fsa 121.00000

This is 'propsettings':

                              sample                                control
1  A01_MF20170623_FA_A01_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
2  A02_MF20170623_FA_A02_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
3  A03_MF20170623_FA_A03_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
4  A05_MF20170623_FA_A05_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
5  A06_MF20170623_FA_A06_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
6  A07_MF20170623_FA_A07_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
7  A08_MF20170623_FA_A08_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
8  A09_MF20170623_FA_A09_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
9  A10_MF20170623_FA_A10_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
10 A11_MF20170623_FA_A11_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
11 A12_MF20170623_FA_A12_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
12 B01_MF20170623_FA_B01_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
13 B02_MF20170623_FA_B02_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
14 B03_MF20170623_FA_B03_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
15 B04_MF20170623_FA_B04_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
16 B05_MF20170623_FA_B05_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
17 B06_MF20170623_FA_B06_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
18 B07_MF20170623_FA_B07_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
19 B08_MF20170623_FA_B08_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
20 C01_MF20170623_FA_C01_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
21 C02_MF20170623_FA_C02_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
22 C05_MF20170623_FA_C05_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
23 C07_MF20170623_FA_C07_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
24 C08_MF20170623_FA_C08_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
25 C09_MF20170623_FA_C09_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
26 C10_MF20170623_FA_C10_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
27 C11_MF20170623_FA_C11_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
28 C12_MF20170623_FA_C12_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
29 D02_MF20170623_FA_D02_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
30 D03_MF20170623_FA_D03_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
31 D04_MF20170623_FA_D04_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
32 D05_MF20170623_FA_D05_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
33 D06_MF20170623_FA_D06_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
34 D08_MF20170623_FA_D08_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
35 D10_MF20170623_FA_D10_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
36 D11_MF20170623_FA_D11_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
37 D12_MF20170623_FA_D12_2017-06-23_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
38 A01_MF20170522_FA_A01_2017-05-22_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
39 C02_MF20170529_FA_C02_2017-05-30_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa
40 B08_MF20170522_FA_B08_2017-05-22_1.fsa B08_MF20170522_FA_B08_2017-05-22_1.fsa

This is a summary of what 'height' looks like:

> str(height)
'data.frame':   1078 obs. of  53 variables:
 $ Dye/SamplePeak                        : chr  "B,65" "B,66" "B,67" "B,68" ...
 $ Marker                                : chr  NA NA NA NA ...
 $ Allele                                : chr  NA NA NA NA ...
 $ Size                                  : num  418 432 435 438 441 ...
 $ Area                                  : num  285 354 300 334 342 385 359 370 410 439 ...
 $ DataPoint                             : num  5665 5827 5859 5890 5924 ...
 $ flank                                 : num  108 108 108 108 108 108 108 108 108 108 ...
 $ correction                            : num  2 2 2 2 2 2 2 2 2 2 ...
 $ start                                 : num  100 100 100 100 100 100 100 100 100 100 ...
 $ end                                   : num  200 200 200 200 200 200 200 200 200 200 ...
 $ control                               : chr  "B08_MF20170522_FA_B08_2017-05-22_1.fsa" "B08_MF20170522_FA_B08_2017-05-22_1.fsa" "B08_MF20170522_FA_B08_2017-05-22_1.fsa" "B08_MF20170522_FA_B08_2017-05-22_1.fsa" ...
 $ iithreshold                           : num  0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 ...
 $ CAG                                   : num  105 110 111 112 113 ...
 $ A01_MF20170623_FA_A01_2017-06-23_1.fsa: num  31 32 32 33 40 37 36 41 45 38 ...
 $ A02_MF20170623_FA_A02_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A03_MF20170623_FA_A03_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A05_MF20170623_FA_A05_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A06_MF20170623_FA_A06_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A07_MF20170623_FA_A07_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A08_MF20170623_FA_A08_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A09_MF20170623_FA_A09_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A10_MF20170623_FA_A10_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A11_MF20170623_FA_A11_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A12_MF20170623_FA_A12_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B01_MF20170623_FA_B01_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B02_MF20170623_FA_B02_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B03_MF20170623_FA_B03_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B04_MF20170623_FA_B04_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B05_MF20170623_FA_B05_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B06_MF20170623_FA_B06_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B07_MF20170623_FA_B07_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B08_MF20170623_FA_B08_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C01_MF20170623_FA_C01_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C02_MF20170623_FA_C02_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C05_MF20170623_FA_C05_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C07_MF20170623_FA_C07_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C08_MF20170623_FA_C08_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C09_MF20170623_FA_C09_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C10_MF20170623_FA_C10_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C11_MF20170623_FA_C11_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C12_MF20170623_FA_C12_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D02_MF20170623_FA_D02_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D03_MF20170623_FA_D03_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D04_MF20170623_FA_D04_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D05_MF20170623_FA_D05_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D06_MF20170623_FA_D06_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D08_MF20170623_FA_D08_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D10_MF20170623_FA_D10_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D11_MF20170623_FA_D11_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ D12_MF20170623_FA_D12_2017-06-23_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ A01_MF20170522_FA_A01_2017-05-22_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ C02_MF20170529_FA_C02_2017-05-30_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...
 $ B08_MF20170522_FA_B08_2017-05-22_1.fsa: num  0 0 0 0 0 0 0 0 0 0 ...


来源: stackexchange

问:
resize showInputDialog textarea?
I'm working with Groovy / Java with Swing, and trying to build JOptionPane showInputDialog frame, which pops to user, and takes user input in text field.

Yes it's simple. But my problem is that showInputDialog text area is by default quite small, and for usability I want it to be bigger.

Does anyone know how to resize textarea?

Example:
This is what I get
small

But this is what I want
big

答:
You might have success with Sergiy Medvynskyy's answer on this question, or at least use it in some way;

```<code>JLabel label = new JLabel("MESSAGE");
label.setFont(new Font("Arial", Font.BOLD, 18));
JOptionPane.showMessageDialog(null,label,"ERROR",JOptionPane.WARNING_MESSAGE);
</code>```

Since JOptionPane accepts a JLabel as parameter, you could experiment a bit by trying to resize that one, and then just passing it on.

Edit:

Assuming you are using Swing, this code should work.
 (You might have to experiment a bit to reach your desired size/layout)

```<code>        TextField tf = new TextField();
        tf.setFont(new Font("Arial", Font.BOLD, 20));

        JPanel jp = new JPanel(new BorderLayout(0, 0));

        JLabel jl = new JLabel("Example Prompt Message!");
        jl.setFont(new Font("Arial", Font.BOLD, 16));

        jp.add(jl, BorderLayout.NORTH);
        jp.add(tf, BorderLayout.SOUTH);

        JOptionPane.showConfirmDialog(null, jp);
</code>```

Result should be something similiar to this. (You can change the dialogue type too of course, though showInputDialog probably won't work, due to it already having a default input line.)

ContentLicense: CC BY-SA 3.0

Title: resize showInputDialog textarea?

Body:
You might have success with Sergiy Medvynskyy's answer on this question, or at least use it in some way;

JLabel label = new JLabel("MESSAGE");
label.setFont(new Font("Arial", Font.BOLD, 18));
JOptionPane.showMessageDialog(null,label,"ERROR",JOptionPane.WARNING_MESSAGE);

Since JOptionPane accepts a JLabel as parameter, you could experiment a bit by trying to resize that one, and then just passing it on.

Edit:

Assuming you are using Swing, this code should work.
 (You might have to experiment a bit to reach your desired size/layout)

        TextField tf = new TextField();
        tf.setFont(new Font("Arial", Font.BOLD, 20));

        JPanel jp = new JPanel(new BorderLayout(0, 0));

        JLabel jl = new JLabel("Example Prompt Message!");
        jl.setFont(new Font("Arial", Font.BOLD, 16));

        jp.add(jl, BorderLayout.NORTH);
        jp.add(tf, BorderLayout.SOUTH);

        JOptionPane.showConfirmDialog(null, jp);

Result should be something similiar to this. (You can change the dialogue type too of course, though showInputDialog probably won't work, due to it already having a default input line.)


问:
making a jar file
I have written a code for client/server application. and I go to

properties>packaging

for making a jar file for it. but when I run my server side and then client side ! it doesn't make the jar file for my application! what should I do? please help me thanks

答:
Use this to make an executable jar

```<code>jar cf jar-file input-file(s)
</code>```

or alternatively you can write ant script to do the same

Document

ContentLicense: CC BY-SA 2.5

Title: making a jar file

Body:
Use this to make an executable jar

jar cf jar-file input-file(s)

or alternatively you can write ant script to do the same

Document


问:
Can't import modules - ImportError: No module named
I've created a new app called engineapp. Inside this app, there is a folder engine which is a Scrapy project.

When I try to import model from storage app inside top.py file, it returns:

```<code>from storage.models import TopItem
ImportError: No module named storage.models
</code>```

Or the similar problem, when I try to import settings of scrapy project:

```<code>from engineapp.engine.engine import settings
</code>```

It returns:

```<code>from engineapp.engine.engine import settings
ImportError: No module named engineapp.engine.engine
</code>```

This is when I run scrapy project from command line.

Both imports created PyCharm itself.

As you can see, I've added __init__() everywhere so python would be able to recognize those files.

Do you know what should I do to be able to import those files?

答:
PyCharm autocomplete relies on different IDE settings. You've marked your realstate_scanner as sources root, so PyCharm can resolve imports for this. From docs:

These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports.

If you can't import some module in python, you should check PATH/PYTHONPATH variables first to make sure python interpreter knows where to find your module.

Body:
I've created a new app called engineapp. Inside this app, there is a folder engine which is a Scrapy project.

When I try to import model from storage app inside top.py file, it returns:

from storage.models import TopItem
ImportError: No module named storage.models

Or the similar problem, when I try to import settings of scrapy project:

from engineapp.engine.engine import settings

It returns:

from engineapp.engine.engine import settings
ImportError: No module named engineapp.engine.engine

This is when I run scrapy project from command line.

Both imports created PyCharm itself.

As you can see, I've added __init__() everywhere so python would be able to recognize those files.

Do you know what should I do to be able to import those files?


问:
x87 FPU computing e powered x, maybe with a Taylor series?
I am trying to compute the function e^x (x is a single-precision float) in x87. To achieve this I am using the Taylor-series (as a reminder: e^x = 1 + (x^1)/1! + (x^2)/2! + ... + (x^n)/n!)

As I'm working with the x87, I can calculate all values in extended precision(80bit instead of the single-precision 32bit).

My realization so far is: I have the summand and the sum in two seperate registers in the FPU (plus some other less significant things). I have a loop, that keeps updating the summand and adding it to the total sum. So in vague pseudo-code:

```<code>loop:
;update my summand
n = n + 1
summand = summand / n
summand = summand * x
;add the summand to the total sum
sum = sum + summand
</code>```

My problem now is the exiting condition of the loop. I wanted to design it in a way that it would exit the loop once adding the summand to the total sum would not impact the value of the sum in single-precision, even though I am figuring out the hard way that implementing such an exit condition is very complex and uses up a lot of instructions -> computing time.

My only kind of okayish idea so far would be:
1.: Get the exponents of the sum and summand via FXTRACT.
If (exp(sum) - exp(summand)) > 23, the summand will not impact the bit representation in single-precision any longer (because the mantissa in single-precision has 23 bits) --> so exit.
2.: Compare the summand with 0, if it is zero it will obviously not impact the result anymore either.

My question is, whether somebody would have any more efficient ideas than me for the exiting condition?

答:
Perhaps the most efficient way to compute ex using the x87 floating-point unit is the following sequence of instructions:

```<code>; Computes e^x via the formula 2^(x * log2(e))
fldl2e                  ; st(0) = log2(e)        <-- load log2(e)
fmul    [x]             ; st(0) = x * log2(e)
fld1                    ; st(0) = 1              <-- load 1
                        ; st(1) = x * log2(e)
fld     st(1)           ; st(0) = x * log2(e)    <-- make copy of intermediate result
                        ; st(1) = 1
                        ; st(2) = x * log2(e)
fprem                   ; st(0) = partial remainder((x * log2(e)) / 1)  <-- call this "rem"
                        ; st(1) = 1
                        ; st(2) = x * log2(e)
f2xm1                   ; st(0) = 2^(rem) - 1
                        ; st(1) = 1
                        ; st(2) = x * log2(e)
faddp   st(1), st(0)    ; st(0) = 2^(rem) - 1 + 1 = 2^(rem)
                        ; st(1) = x * log2(e)
fscale                  ; st(0) = 2^(rem) * 2^(trunc(x * log2(e)))
                        ; st(1) = x * log2(e)
fstp    st(1)
</code>```

The result is left in st(0).

If you already have the input, x, on the floating-point stack, then you can modify the sequence of instructions slightly:

```<code>; st(0) == x
fldl2e
fmulp   st(1), st(0)
fld1
fld     st(1)
fprem
f2xm1
faddp   st(1), st(0)
fscale
fstp    st(1)
</code>```

This multiplies x by log2e, finds the remainder of that divided by 1, exponentiates 2 to the power of that value (f2xm1 also subtracts 1, then we add 1 back in), and finally scales that by x × log2e.

An alternative implementation—essentially that suggested by fuz and reminiscent of the code generated by MSVC for the exp function from the C standard library—is the following:

```<code>; st(0) == x
fldl2e
fmulp    st(1), st(0)
fld      st(0)
frndint
fsub     st(1), st(0)
fxch                         ; pairable with FSUB on Pentium (P5)
f2xm1
fld1
faddp    st(1), st(0)
fscale
fstp     st(1)
</code>```

The primary difference being the use of frndint and fsub to get a value in the range of −1.0 to +1.0, as required by f2xm1, as opposed to using fprem to get the remainder after a division by 1.

To get an idea of the relative costs of these instructions, we'll pull data from Agner Fog's instruction tables. A "?" indicates that the corresponding data is unavailable.

```<code>Instruction      AMD K7      AMD K8      AMD K10    Bulldozer    Piledriver    Ryzen
-------------------------------------------------------------------------------------------
FLD/FLD1/FLDL2E  [all very fast, 1-cycle instructions, with a reciprocal throughput of 1]
FADD(P)/FSUB(P)  1/4/1       1/4/1       1/4/1      1/5-6/1      1/5-6/1       1/5/1
FMUL(P)          1/4/1       1/4/1       1/4/1      1/5-6/1      1/5-6/1       1/5/1
FPREM            1/7-10/8    1/7-10/8    1/?/7      1/19-62/?    1/17-60/?     2/?/12-50
FRNDINT          5/10/3      5/10/3      6/?/37     1/4/1        1/4/1         1/4/3
FSCALE           5/8/?       5/8/?       5/9/29     8/52/?       8/44/5        8/9/4
F2XM1            8/27/?      53/126/?    8/65/30?   10/64-71/?   10/60-73/?    10/50/?
</code>```

The notation used above is "ops／latency／reciprocal throughput".

```<code>Instruction      8087     287      387      486      P5         P6        PM      Nehalem
-------------------------------------------------------------------------------------------
FLD              17-22    17-22    14       4        1/0        1/?       1/1      1/1
FLD1             15-21    15-21    24       4        2/0        2/?       2/?      2/?
FLDL2E           15-21    15-21    40       8        5/2        2/?       2/?      2/?
FADD(P)/FSUB(P)  70-100   70-100   23-34    8-20     3/2        1/3       1/3      1/3
FMUL(P)          90-145   90-145   29-57    16       3/2        1/5       1/5      1/5
FPREM            15-190   15-190   74-155   70-138   16-64 (2)  23/?      26/37    25/14
FRNDINT          16-50    16-50    66-80    21-30    9-20 (0)   30/?      15/19    17/22
FSCALE           32-38    32-38    67-86    30-32    20-32 (5)  56/?      28/43    24/12
F2XM1            310-630  310-630  211-476  140-279  13-57 (2)  17-48/66  ~20/?    19/58
</code>```

For pre-P5, the value is just cycle counts. For P5, the notation is cycle counts, with the parenthetical indicating overlap with other FP instructions. For P6 and later, the notation is µops／latency.

Clearly, f2xm1 is a slow, expensive instruction, but this used by both implementations and hard to avoid. As slow as it is, it's still way faster than implementing this as a loop.

(One possible way around the slow f2xm1 instruction—if you are willing to sacrifice code size for speed—would be a look-up table-based approach. You can find several papers published on this if you search, although most of them are behind paywalls. :-( Here's one publicly-accessible reference. In fact, this is probably what an optimized math library written in C would do for its implementation of exp. Ben Voigt wrote similar code in C#. The basic question is here, as always, are you optimizing for size or speed? In other words, are you calling this function frequently and need it to be as fast as possible? Or are you calling it infrequently and just need it to be precise, without consuming too much space in the binary and potentially slowing down other code on the hot path by evicting it from the cache.)

But what about fprem vs. frndint?

Well, on AMD CPUs, fprem pretty consistently decodes to fewer operations than frndint, although frndint is getting faster than fprem on modern AMD CPUs. Then again, I'd say that's irrelevant, because on those CPUs, you'd be writing SSE2/AVX code, not using the legacy x87 FPU. (These instructions also get slower on later model Intel microarchitectures, like Sandy Bridge, Haswell, etc., but the same argument applies there, so I've ignored those altogether in compiling the data.) What really matters is how this code performs on older CPUs, and on older CPUs, the version that uses fprem looks like a clear winner.

On Intel CPUs, however, the reverse appears to be true: frndint is generally faster than fprem, except on P6 (Pentium Pro, Pentium II, and Pentium III).

But, we're not just talking about frndint vs fprem—we're actually talking about fprndint+fsub vs. fprem. fsub is a relatively fast instruction, but it starts to get difficult to predict just what performance this code is going to have without executing it and timing it. Cycle counts can only tell us so much, and the fprem sequence is shorter overall (fewer instructions and, more importantly, fewer bytes—18 vs. 22), which can mean substantial speed improvements.

Either implementation is good, if you don't want to bother benchmarking it, or you want something generic that runs well on all CPUs. Neither of them are going to set the world on fire in terms of performance, but as I said before, both will be significantly faster than a loop that tries to calculate a Taylor series. The overhead there is what will kill your performance.

ContentLicense: CC BY-SA 4.0

Body:
I am trying to compute the function e^x (x is a single-precision float) in x87. To achieve this I am using the Taylor-series (as a reminder: e^x = 1 + (x^1)/1! + (x^2)/2! + ... + (x^n)/n!)

As I'm working with the x87, I can calculate all values in extended precision(80bit instead of the single-precision 32bit).

My realization so far is: I have the summand and the sum in two seperate registers in the FPU (plus some other less significant things). I have a loop, that keeps updating the summand and adding it to the total sum. So in vague pseudo-code:

loop:
;update my summand
n = n + 1
summand = summand / n
summand = summand * x
;add the summand to the total sum
sum = sum + summand

My problem now is the exiting condition of the loop. I wanted to design it in a way that it would exit the loop once adding the summand to the total sum would not impact the value of the sum in single-precision, even though I am figuring out the hard way that implementing such an exit condition is very complex and uses up a lot of instructions -> computing time.

My only kind of okayish idea so far would be:
1.: Get the exponents of the sum and summand via FXTRACT.
If (exp(sum) - exp(summand)) > 23, the summand will not impact the bit representation in single-precision any longer (because the mantissa in single-precision has 23 bits) --> so exit.
2.: Compare the summand with 0, if it is zero it will obviously not impact the result anymore either.

My question is, whether somebody would have any more efficient ideas than me for the exiting condition?

Body:
Perhaps the most efficient way to compute ex using the x87 floating-point unit is the following sequence of instructions:

; Computes e^x via the formula 2^(x * log2(e))
fldl2e                  ; st(0) = log2(e)        <-- load log2(e)
fmul    [x]             ; st(0) = x * log2(e)
fld1                    ; st(0) = 1              <-- load 1
                        ; st(1) = x * log2(e)
fld     st(1)           ; st(0) = x * log2(e)    <-- make copy of intermediate result
                        ; st(1) = 1
                        ; st(2) = x * log2(e)
fprem                   ; st(0) = partial remainder((x * log2(e)) / 1)  <-- call this "rem"
                        ; st(1) = 1
                        ; st(2) = x * log2(e)
f2xm1                   ; st(0) = 2^(rem) - 1
                        ; st(1) = 1
                        ; st(2) = x * log2(e)
faddp   st(1), st(0)    ; st(0) = 2^(rem) - 1 + 1 = 2^(rem)
                        ; st(1) = x * log2(e)
fscale                  ; st(0) = 2^(rem) * 2^(trunc(x * log2(e)))
                        ; st(1) = x * log2(e)
fstp    st(1)

The result is left in st(0).

If you already have the input, x, on the floating-point stack, then you can modify the sequence of instructions slightly:

; st(0) == x
fldl2e
fmulp   st(1), st(0)
fld1
fld     st(1)
fprem
f2xm1
faddp   st(1), st(0)
fscale
fstp    st(1)

This multiplies x by log2e, finds the remainder of that divided by 1, exponentiates 2 to the power of that value (f2xm1 also subtracts 1, then we add 1 back in), and finally scales that by x × log2e.

An alternative implementation—essentially that suggested by fuz and reminiscent of the code generated by MSVC for the exp function from the C standard library—is the following:

; st(0) == x
fldl2e
fmulp    st(1), st(0)
fld      st(0)
frndint
fsub     st(1), st(0)
fxch                         ; pairable with FSUB on Pentium (P5)
f2xm1
fld1
faddp    st(1), st(0)
fscale
fstp     st(1)

The primary difference being the use of frndint and fsub to get a value in the range of −1.0 to +1.0, as required by f2xm1, as opposed to using fprem to get the remainder after a division by 1.

To get an idea of the relative costs of these instructions, we'll pull data from Agner Fog's instruction tables. A "?" indicates that the corresponding data is unavailable.

Instruction      AMD K7      AMD K8      AMD K10    Bulldozer    Piledriver    Ryzen
-------------------------------------------------------------------------------------------
FLD/FLD1/FLDL2E  [all very fast, 1-cycle instructions, with a reciprocal throughput of 1]
FADD(P)/FSUB(P)  1/4/1       1/4/1       1/4/1      1/5-6/1      1/5-6/1       1/5/1
FMUL(P)          1/4/1       1/4/1       1/4/1      1/5-6/1      1/5-6/1       1/5/1
FPREM            1/7-10/8    1/7-10/8    1/?/7      1/19-62/?    1/17-60/?     2/?/12-50
FRNDINT          5/10/3      5/10/3      6/?/37     1/4/1        1/4/1         1/4/3
FSCALE           5/8/?       5/8/?       5/9/29     8/52/?       8/44/5        8/9/4
F2XM1            8/27/?      53/126/?    8/65/30?   10/64-71/?   10/60-73/?    10/50/?

The notation used above is "ops／latency／reciprocal throughput".

Instruction      8087     287      387      486      P5         P6        PM      Nehalem
-------------------------------------------------------------------------------------------
FLD              17-22    17-22    14       4        1/0        1/?       1/1      1/1
FLD1             15-21    15-21    24       4        2/0        2/?       2/?      2/?
FLDL2E           15-21    15-21    40       8        5/2        2/?       2/?      2/?
FADD(P)/FSUB(P)  70-100   70-100   23-34    8-20     3/2        1/3       1/3      1/3
FMUL(P)          90-145   90-145   29-57    16       3/2        1/5       1/5      1/5
FPREM            15-190   15-190   74-155   70-138   16-64 (2)  23/?      26/37    25/14
FRNDINT          16-50    16-50    66-80    21-30    9-20 (0)   30/?      15/19    17/22
FSCALE           32-38    32-38    67-86    30-32    20-32 (5)  56/?      28/43    24/12
F2XM1            310-630  310-630  211-476  140-279  13-57 (2)  17-48/66  ~20/?    19/58

For pre-P5, the value is just cycle counts. For P5, the notation is cycle counts, with the parenthetical indicating overlap with other FP instructions. For P6 and later, the notation is µops／latency.

Clearly, f2xm1 is a slow, expensive instruction, but this used by both implementations and hard to avoid. As slow as it is, it's still way faster than implementing this as a loop.

(One possible way around the slow f2xm1 instruction—if you are willing to sacrifice code size for speed—would be a look-up table-based approach. You can find several papers published on this if you search, although most of them are behind paywalls. :-( Here's one publicly-accessible reference. In fact, this is probably what an optimized math library written in C would do for its implementation of exp. Ben Voigt wrote similar code in C#. The basic question is here, as always, are you optimizing for size or speed? In other words, are you calling this function frequently and need it to be as fast as possible? Or are you calling it infrequently and just need it to be precise, without consuming too much space in the binary and potentially slowing down other code on the hot path by evicting it from the cache.)

But what about fprem vs. frndint?

Well, on AMD CPUs, fprem pretty consistently decodes to fewer operations than frndint, although frndint is getting faster than fprem on modern AMD CPUs. Then again, I'd say that's irrelevant, because on those CPUs, you'd be writing SSE2/AVX code, not using the legacy x87 FPU. (These instructions also get slower on later model Intel microarchitectures, like Sandy Bridge, Haswell, etc., but the same argument applies there, so I've ignored those altogether in compiling the data.) What really matters is how this code performs on older CPUs, and on older CPUs, the version that uses fprem looks like a clear winner.

On Intel CPUs, however, the reverse appears to be true: frndint is generally faster than fprem, except on P6 (Pentium Pro, Pentium II, and Pentium III).

But, we're not just talking about frndint vs fprem—we're actually talking about fprndint+fsub vs. fprem. fsub is a relatively fast instruction, but it starts to get difficult to predict just what performance this code is going to have without executing it and timing it. Cycle counts can only tell us so much, and the fprem sequence is shorter overall (fewer instructions and, more importantly, fewer bytes—18 vs. 22), which can mean substantial speed improvements.

Either implementation is good, if you don't want to bother benchmarking it, or you want something generic that runs well on all CPUs. Neither of them are going to set the world on fire in terms of performance, but as I said before, both will be significantly faster than a loop that tries to calculate a Taylor series. The overhead there is what will kill your performance.


问:
Bootstrap table not working good with ng-repeat
```<code><div ng-controller="reportCtrl">
      <table class="table table-hover">
          <thead class="row col-md-3">
              <tr class="row">
                  <th class="col-md-6">Key </th>
                  <th class="col-md-6"> Value</th>
              </tr>
          </thead>
          <tbody ng-repeat="param in params" class="row col-md-3">
              <tr class="row">
                  <td class="col-md-6 info">{{param.key}}</td>
                  <td class="col-md-6 info">{{param.val}}</td>
              </tr>
          </tbody>
      </table>
</div>
</code>```

I have this table, when i'm using the bootstrap grid system with ng-repeat the results are very strange..

I've tried to play with the grid system but it dosent seem that it helps..

答:
You may try this code
By removing the col-md-3
and style the table with width:auto

```<code><div ng-controller="reportCtrl">
  <table class="table table-hover" style="width:auto">
    <thead class="row ">
      <tr class="row">
        <th class="col-md-6">Key </th>
        <th class="col-md-6"> Value</th>
      </tr>
    </thead>
    <tbody ng-repeat="param in params" class="row ">
      <tr class="row">
        <td class="col-md-6 info">{{param.key}}</td>
        <td class="col-md-6 info">{{param.val}}</td>
      </tr>
    </tbody>
    <tbody ng-repeat="param in params" class="row ">
      <tr class="row">
        <td class="col-md-6 info">{{param.key}}</td>
        <td class="col-md-6 info">{{param.val}}</td>
      </tr>
    </tbody>
  </table>
</div>
</code>```

Body:
                  Key
                   Value

                  {{param.key}}
                  {{param.val}}

I have this table, when i'm using the bootstrap grid system with ng-repeat the results are very strange..

I've tried to play with the grid system but it dosent seem that it helps..

Body:
You may try this code
By removing the col-md-3
and style the table with width:auto

<div ng-controller="reportCtrl">
  <table class="table table-hover" style="width:auto">
    <thead class="row ">
      <tr class="row">
        <th class="col-md-6">Key </th>
        <th class="col-md-6"> Value</th>
      </tr>
    </thead>
    <tbody ng-repeat="param in params" class="row ">
      <tr class="row">
        <td class="col-md-6 info">{{param.key}}</td>
        <td class="col-md-6 info">{{param.val}}</td>
      </tr>
    </tbody>
    <tbody ng-repeat="param in params" class="row ">
      <tr class="row">
        <td class="col-md-6 info">{{param.key}}</td>
        <td class="col-md-6 info">{{param.val}}</td>
      </tr>
    </tbody>
  </table>
</div>


问:
Set Route Attribute with a String Parameter
I'm attempting to create a Web API method with a custom route that takes in a String parameter, but this appears to error upon application start - so I don't even need to hit the route or the method for the application to break.

```<code>// Pass String to TMS and Return an ID
[System.Web.Http.HttpGet]
[System.Web.Http.Route("api/job/import/{import:string}")]
public int TmsImport(string import)
{
    var TmsId = 0;
    if (import.Length > 0)
    {
        TmsId = 1;
    }
    return (TmsId);
}
</code>```

The application breaks on the following method in Global.asx:

```<code>protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}
</code>```

This same method works when I pass an int to the method, but it doesn't seems to able to recognise the name 'string' in the parameter. I did see an answer to a similar question here: call to web api with string parameter - but I can't figure out where the change would be from that answer?  If it would be in the call from the JS or in the route config?

答:
You just need to remove :string from {import:string}and try to connect to

"api/job/import/somestring"

MSDN article

Body:
I'm attempting to create a Web API method with a custom route that takes in a String parameter, but this appears to error upon application start - so I don't even need to hit the route or the method for the application to break.

// Pass String to TMS and Return an ID
[System.Web.Http.HttpGet]
[System.Web.Http.Route("api/job/import/{import:string}")]
public int TmsImport(string import)
{
    var TmsId = 0;
    if (import.Length > 0)
    {
        TmsId = 1;
    }
    return (TmsId);
}

The application breaks on the following method in Global.asx:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

This same method works when I pass an int to the method, but it doesn't seems to able to recognise the name 'string' in the parameter. I did see an answer to a similar question here: call to web api with string parameter - but I can't figure out where the change would be from that answer?  If it would be in the call from the JS or in the route config?


问:
Can i add an ActionListener() inside a main method or any method?
```<code>public void Message() {
@Override
public void actionPerformed(ActionEvent e) {
    JOptionPane.showMessageDialog(null, "Hello it is working");
}

}
</code>```

<p>//Message() is a method inside a class.</p>

答:
Since Java 8 methods can be nested using lambdas.
Can methods in java be nested[...]?
No, that's not possible.

The closest thing you can get is:

```<code>class Name {
    void methodOne() {
        class InnerClass {
           void methodTwo() {
           }
         }
     }
 }
</code>```

That is, a second method defined in a inner class defined in a method.

You can declare the method static inside the inner class, so you do not have to call new

Body:
public void Message() {
@Override
public void actionPerformed(ActionEvent e) {
    JOptionPane.showMessageDialog(null, "Hello it is working");
}

}

//Message() is a method inside a class.

Body:
Since Java 8 methods can be nested using lambdas.
Can methods in java be nested[...]?
No, that's not possible.

The closest thing you can get is:

class Name {
    void methodOne() {
        class InnerClass {
           void methodTwo() {
           }
         }
     }
 }

That is, a second method defined in a inner class defined in a method.

You can declare the method static inside the inner class, so you do not have to call new


问:
How can I send a SharePoint alert only when a particular column is changed?
I need to trigger alert, if value of particular column (say marks<8, send mail to manager) changes.

What should I do?

答:
You can implement an event handler on the list to do this :
http://www.c-sharpcorner.com/UploadFile/Chandresh.P/EventHandler04292009070324AM/EventHandler.aspx</p>


问:
Datatables.net error on Ajax load
I am working on a simple ajax example in datatables and it is not working and I am at a loss to explain it. I have a simple table as follows:

```<code><table id="tblAddresses">
  <thead>
    <tr>
        <th>Street Address</th>
        <th>City</th>
        <th>State</th>
        <th>Zip Code</th>
    </tr>
 </thead>
 <tfoot>
    <tr>
        <th>Street Address</th>
        <th>City</th>
        <th>State</th>
        <th>Zip Code</th>
        </tr>
 </tfoot>
</table>
</code>```

I have a json data source with data that looks like this (I prettied it up a bit for display here but the file is one long line with no line breaks).

```<code>{"data":[{"street":"19 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"27 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"31 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"35 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"39 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"49 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"}]}
</code>```

Finally, I load it in my document ready function:

```<code><script type="text/javascript">
    $(document).ready(function(){
        $("#tblAddresses").DataTable({
            "ajax" : {
                "url" : "/json/07055.json",
                "columns" : [{"data":"street"},
                             {"data":"city"},
                             {"data":"state"},
                             {"data":"postcode"}]
            }
        });
    });
</script>
</code>```

When I load the page, I see the ajax call. I can see the data accepted by the browser but DataTables is giving me an error:

DataTables warning: table id=tblAddresses - Requested unknown parameter '0' for row 0, column 0.

I have worked with ajax many times before albeit never loading from a static data file. I can't find the error in the JSON or Javascript.

答:
You are binding data in wrong way. You need to bind columns after ajax method, like bellow,

```<code>$("#tblAddresses").DataTable({
        "ajax" : {
            "url" : "/json/07055.json",
            "type": "Get"
        }, //Here end of ajax method. Now you can bind the columns
         "columns" : [{"data":"street"},
                      {"data":"city"},
                      {"data":"state"},
                      {"data":"postcode"}]
        });
</code>```

Hope it helps!

Title: Datatables.net error on Ajax load

Body:
I am working on a simple ajax example in datatables and it is not working and I am at a loss to explain it. I have a simple table as follows:

        Street Address
        City
        State
        Zip Code

        Street Address
        City
        State
        Zip Code

I have a json data source with data that looks like this (I prettied it up a bit for display here but the file is one long line with no line breaks).

{"data":[{"street":"19 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"27 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"31 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"35 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"39 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"},
{"street":"49 Brook Avenue","city":"PASSAIC","state":"NJ","postcode":"07055"}]}

Finally, I load it in my document ready function:

When I load the page, I see the ajax call. I can see the data accepted by the browser but DataTables is giving me an error:

DataTables warning: table id=tblAddresses - Requested unknown parameter '0' for row 0, column 0.

I have worked with ajax many times before albeit never loading from a static data file. I can't find the error in the JSON or Javascript.

Body:
You are binding data in wrong way. You need to bind columns after ajax method, like bellow,

$("#tblAddresses").DataTable({
        "ajax" : {
            "url" : "/json/07055.json",
            "type": "Get"
        }, //Here end of ajax method. Now you can bind the columns
         "columns" : [{"data":"street"},
                      {"data":"city"},
                      {"data":"state"},
                      {"data":"postcode"}]
        });

Hope it helps!


问:
Dividing a number by instances of my class in Python
I have a class called Time, and I need to implement a Frequency class. How can I implement dividing ints or floats by an instance of Time to get an instance of Frequency ?

I already know about __div__, __truediv__, __floordiv__ and other Python special methods, and I already use them in my code to divide instances of classes by numbers or instances of other classes, but I cannot find a way to divide a number by an instance of my class.

Is it possible to implement dividing a number by an instance of a class in Python ?

答:
The __rtruediv__ method is what you're looking for.
When x / y is executed, if type(x) does not implement a __div__(self, other) method where other can be of class type(y), then type(y).__rtruediv__(y, x) is executed, and its result is returned.

Usage:

```<code>class Foo:
    def __init__(self, x):
        self.x = x

    def __truediv__(self, other):
        return self.x / other

    def __rtruediv__(self, other):
        return other / self.x
</code>```

```<code>>>> f = Foo(10)
>>> f / 10
1.0
>>> 10 / f
1.0</code>```

Body:
The __rtruediv__ method is what you're looking for.
When x / y is executed, if type(x) does not implement a __div__(self, other) method where other can be of class type(y), then type(y).__rtruediv__(y, x) is executed, and its result is returned.

Usage:

class Foo:
    def __init__(self, x):
        self.x = x

    def __truediv__(self, other):
        return self.x / other

    def __rtruediv__(self, other):
        return other / self.x

>>> f = Foo(10)
>>> f / 10
1.0
>>> 10 / f
1.0


问:
Set up a scheduler that triggers every 10 minutes in C# ASP.NET MVC 5
I'm trying to set up a scheduler that will trigger every 10 minutes, activating a database query. I can't find any good tutorials on this and some advice would be great. Language is C#, web framework is ASP.NET MVC 5.</p>

答:
I have done that using CacheItemRemovedCallback

in your Global.asax file add this lines at the end of your Application_Start()

```<code>if (null != HttpContext.Current.Cache["cacheKey"]) return;

HttpRuntime.Cache.Add("cacheKey", "cacheValue", null, DateTime.MaxValue, TimeSpan.FromSeconds(600), CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(CacheItemRemovedCallback));
</code>```

This will create new cache item that will trigger after 10 minutes

add the following method in Global.asax

```<code>public void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
     //Your code here, call database procedures ...
     HttpRuntime.Cache.Add("cacheKey", "cacheValue", null, DateTime.MaxValue, TimeSpan.FromSeconds(600), CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(CacheItemRemovedCallback));
}
</code>```

What happens here is that you are creating new cache and remove it after 10 minutes, when removing the cache the event CacheItemRemovedCallback is called so you can do your work and then create new cache that will be removed in 10 minutes.

Body:
I have done that using CacheItemRemovedCallback

in your Global.asax file add this lines at the end of your Application_Start()

if (null != HttpContext.Current.Cache["cacheKey"]) return;

HttpRuntime.Cache.Add("cacheKey", "cacheValue", null, DateTime.MaxValue, TimeSpan.FromSeconds(600), CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(CacheItemRemovedCallback));

This will create new cache item that will trigger after 10 minutes

add the following method in Global.asax

public void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
     //Your code here, call database procedures ...
     HttpRuntime.Cache.Add("cacheKey", "cacheValue", null, DateTime.MaxValue, TimeSpan.FromSeconds(600), CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(CacheItemRemovedCallback));
}

What happens here is that you are creating new cache and remove it after 10 minutes, when removing the cache the event CacheItemRemovedCallback is called so you can do your work and then create new cache that will be removed in 10 minutes.


问:
iTunes account creation not allowed when trying to test In-App Purchases
I have an app in the App Store and I want to test in-app purchases. I followed the steps described on the Apple website but I'm facing some problems.

When I try to log in in the iTunes in my application or in Settings/iTunes & App Stores, I receive the error:

iTunes account creation not allowed.

This Apple ID cannot be used with the iTunes Store at this time. Please try again later

I searched the internet but I found nothing about this. It seems like I am the first to encounter this kind of error.

答:
I had this issue. I tried changing existing Sandbox tester account password and tried to login on device...but didn't worked.

Even iPhone device restart didn't worked [last option that most developer follows ;-) ]

I thought it takes time to update this newly created Sandbox testing on DB. So left the login option as it is on device

Now, after launching app that contains In App purchase. Press on button that is responsible for In App purchase. This will prompt for Apple credentials. Enter the Sandbox tester credentials here.

Voila this worked for me!