Rbin-file used for this script: ~/Data/CoordSys_FLT1-7_50x.Rbin


1. Goal of the script

This script calculates the shift in X/Y directions from the first scan.

This script computes standard descriptive statistics for each group.
The groups are based on:

  • Microscope

It computes the following statistics:

  • n (sample size = length)
  • smallest value (min)
  • largest value (max)
  • mean
  • median
  • standard deviation (sd)
dir.stats <- "Summary-stats"
dir.create(dir.stats, showWarnings=FALSE)

The results will be written to XLSX-files in a subfolder “Summary-stats” within the directory of the present Rmd file, i.e. “~/Summary-stats”.


2. Load packages

library(openxlsx)
library(R.utils)
library(tools)
library(doBy)

3. Load data into R object

imp.info <- file.info(params$data)
imp.data <- loadObject(params$data)

The imported file is: “~/Data/CoordSys_FLT1-7_50x.Rbin”
Its modification, ‘last status change’ (= ‘creation’ on Windows) and last access times are, respectively: “2019-01-03 10:28:47”, “2019-01-03 10:28:47” and “2019-01-03 10:28:47”.


4. Define numeric variables

num.var.topo <- 19:47
num.var.shift <- 54:55
names(imp.data)[num.var.topo]
 [1] "Sq"   "Ssk"  "Sku"  "Sp"   "Sv"   "Sz"   "Sa"   "Smr"  "Smc"  "Sxp" 
[11] "Sal"  "Str"  "Std"  "Sdq"  "Sdr"  "Vm"   "Vv"   "Vmp"  "Vmc"  "Vvc" 
[21] "Vvv"  "Sk"   "Spk"  "Svk"  "Smr1" "Smr2" "Spq"  "Svq"  "Smq" 
names(imp.data)[num.var.shift]
[1] "Xshift" "Yshift"

6. Compute summary statistics

6.1. Create function to compute the statistics at once

minmaxmeanmedsd <- function(x){
    y <- x[!is.na(x)]
    min_test <- min(y)
    max_test <- max(y)
    mean_test <- mean(y)
    med_test <- median(y)
    sd_test <- sd(y)
    out <- c(min_test, max_test, mean_test, med_test, sd_test)
    names(out) <- c("min", "max", "mean", "median", "sd")
    return(out)
}

6.2. Compute the summary statistics in groups

6.2.1 ISO parameters
#min, max, mean, median, sd
topo.mmmms <- summaryBy(.~Microscope, data=imp.data[c("Microscope",names(imp.data)[num.var.topo])], FUN=minmaxmeanmedsd)

#n for first variable
topo.n <- summaryBy(as.formula(paste0(names(imp.data[num.var.topo])[1],"~Microscope")), data=imp.data, FUN=length)

#combine n, min, max, mean, median and sd
topo <- merge(topo.n, topo.mmmms)
str(topo)
'data.frame':   2 obs. of  137 variables:
 $ Microscope : Factor w/ 2 levels "LSM","Sneox": 1 2
 $ Sq.length  : int  10 10
 $ Sq.min     : num  1.16 1.16
 $ Sq.max     : num  1.49 1.79
 $ Sq.mean    : num  1.31 1.56
 $ Sq.median  : num  1.31 1.62
 $ Sq.sd      : num  0.089 0.207
 $ Ssk.min    : num  -0.00297 0.02434
 $ Ssk.max    : num  0.145 0.357
 $ Ssk.mean   : num  0.0769 0.1475
 $ Ssk.median : num  0.0852 0.1325
 $ Ssk.sd     : num  0.0503 0.1061
 $ Sku.min    : num  3.1 3.95
 $ Sku.max    : num  4.42 4.82
 $ Sku.mean   : num  3.98 4.39
 $ Sku.median : num  3.95 4.41
 $ Sku.sd     : num  0.408 0.286
 $ Sp.min     : num  5.19 6.09
 $ Sp.max     : num  6.24 7.04
 $ Sp.mean    : num  6 6.41
 $ Sp.median  : num  6.07 6.29
 $ Sp.sd      : num  0.307 0.324
 $ Sv.min     : num  3.91 4.15
 $ Sv.max     : num  7.14 7.6
 $ Sv.mean    : num  5.45 6.85
 $ Sv.median  : num  5.86 7.26
 $ Sv.sd      : num  1.16 1.1
 $ Sz.min     : num  9.11 10.23
 $ Sz.max     : num  13.2 14.2
 $ Sz.mean    : num  11.4 13.3
 $ Sz.median  : num  12 13.7
 $ Sz.sd      : num  1.39 1.24
 $ Sa.min     : num  0.922 0.875
 $ Sa.max     : num  1.12 1.37
 $ Sa.mean    : num  1.02 1.17
 $ Sa.median  : num  1.02 1.2
 $ Sa.sd      : num  0.0546 0.1531
 $ Smr.min    : num  0.0463 0.1238
 $ Smr.max    : num  0.293 0.558
 $ Smr.mean   : num  0.162 0.378
 $ Smr.median : num  0.148 0.438
 $ Smr.sd     : num  0.0723 0.1864
 $ Smc.min    : num  1.49 1.34
 $ Smc.max    : num  1.72 2.29
 $ Smc.mean   : num  1.59 1.83
 $ Smc.median : num  1.58 1.84
 $ Smc.sd     : num  0.0613 0.2942
 $ Sxp.min    : num  2.33 2.27
 $ Sxp.max    : num  3.04 3.53
 $ Sxp.mean   : num  2.63 3.11
 $ Sxp.median : num  2.6 3.29
 $ Sxp.sd     : num  0.193 0.398
 $ Sal.min    : num  12.4 13.6
 $ Sal.max    : num  14.6 17.9
 $ Sal.mean   : num  14 15.4
 $ Sal.median : num  14.1 14.8
 $ Sal.sd     : num  0.638 1.405
 $ Str.min    : num  0.512 0.635
 $ Str.max    : num  0.827 0.791
 $ Str.mean   : num  0.718 0.73
 $ Str.median : num  0.72 0.745
 $ Str.sd     : num  0.0904 0.0536
 $ Std.min    : num  69 3.76
 $ Std.max    : num  157 144
 $ Std.mean   : num  100.1 65.3
 $ Std.median : num  90.7 54.3
 $ Std.sd     : num  29.1 59.4
 $ Vm.min     : num  0.0546 0.0766
 $ Vm.max     : num  0.0974 0.1237
 $ Vm.mean    : num  0.0727 0.1054
 $ Vm.median  : num  0.0727 0.1118
 $ Vm.sd      : num  0.0117 0.0175
 $ Vv.min     : num  1.55 1.42
 $ Vv.max     : num  1.81 2.39
 $ Vv.mean    : num  1.66 1.94
 $ Vv.median  : num  1.65 1.96
 $ Vv.sd      : num  0.0725 0.3067
 $ Vmp.min    : num  0.0546 0.0766
 $ Vmp.max    : num  0.0974 0.1237
 $ Vmp.mean   : num  0.0727 0.1054
 $ Vmp.median : num  0.0727 0.1118
 $ Vmp.sd     : num  0.0117 0.0175
 $ Vmc.min    : num  1.039 0.957
 $ Vmc.max    : num  1.2 1.47
 $ Vmc.mean   : num  1.11 1.24
 $ Vmc.median : num  1.12 1.27
 $ Vmc.sd     : num  0.043 0.151
 $ Vvc.min    : num  1.41 1.29
 $ Vvc.max    : num  1.63 2.18
 $ Vvc.mean   : num  1.5 1.74
 $ Vvc.median : num  1.5 1.75
 $ Vvc.sd     : num  0.0576 0.2833
 $ Vvv.min    : num  0.133 0.136
 $ Vvv.max    : num  0.187 0.219
 $ Vvv.mean   : num  0.156 0.192
 $ Vvv.median : num  0.153 0.202
 $ Vvv.sd     : num  0.0157 0.0265
 $ Sk.min     : num  2.24 1.94
 $ Sk.max     : num  2.51 2.65
  [list output truncated]
6.2.2 X/Y shifts
#min, max, mean, median, sd
shift.mmmms <- summaryBy(.~Microscope, data=imp.data[-c(1,11), c("Microscope",names(imp.data)[num.var.shift])], FUN=minmaxmeanmedsd)

#n for first variable
shift.n <- summaryBy(as.formula(paste0(names(imp.data[num.var.shift])[1],"~Microscope")), data=imp.data[-c(1,11),], FUN=length)

#combine n, min, max, mean, median and sd
shift <- merge(shift.n, shift.mmmms)
str(shift)
'data.frame':   2 obs. of  12 variables:
 $ Microscope   : Factor w/ 2 levels "LSM","Sneox": 1 2
 $ Xshift.length: int  9 9
 $ Xshift.min   : num  18.85 5.01
 $ Xshift.max   : num  88.7 129.3
 $ Xshift.mean  : num  41.8 58.5
 $ Xshift.median: num  33.3 50.2
 $ Xshift.sd    : num  20.4 41.4
 $ Yshift.min   : num  1.573 0.789
 $ Yshift.max   : num  52.1 61.4
 $ Yshift.mean  : num  23.6 28.1
 $ Yshift.median: num  18.1 27.1
 $ Yshift.sd    : num  17 20.4

7. Write results to XLSX

7.1. Format file output name

file.out <- paste0(dir.stats, "/", basename(file_path_sans_ext(row.names(imp.info))), "_summary-stats.xlsx")
file.out
[1] "Summary-stats/CoordSys_FLT1-7_50x_summary-stats.xlsx"

7.2. Write to XLSX

write.xlsx(list(ISO=topo, XYshift=shift), file=file.out)
xlsx.info <- file.info(file.out)

The exported XLSX-file is: “CoordSys_FLT1-7_50x_summary-stats.xlsx”
It is saved in “~/Summary-stats”
Its modification, ‘last status change’ (= ‘creation’ on Windows) and last access times are, respectively: “2019-01-03 10:34:13”, “2019-01-03 10:34:13” and “2019-01-03 10:34:13”.


sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] tools     stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] doBy_4.6-2        R.utils_2.7.0     R.oo_1.22.0       R.methodsS3_1.7.1
[5] openxlsx_4.1.0   

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0       bindr_0.1.1      knitr_1.21       magrittr_1.5    
 [5] MASS_7.3-51.1    tidyselect_0.2.5 lattice_0.20-38  R6_2.3.0        
 [9] rlang_0.3.0.1    stringr_1.3.1    plyr_1.8.4       dplyr_0.7.8     
[13] grid_3.5.2       xfun_0.4         htmltools_0.3.6  yaml_2.2.0      
[17] digest_0.6.18    assertthat_0.2.0 tibble_1.4.2     crayon_1.3.4    
[21] bindrcpp_0.2.2   Matrix_1.2-15    zip_1.0.0        purrr_0.2.5     
[25] glue_1.3.0       evaluate_0.12    rmarkdown_1.11   stringi_1.2.4   
[29] pillar_1.3.1     compiler_3.5.2   pkgconfig_2.0.2 

END OF SCRIPT