Functions

When using the JGR, the menu selections are translated into R commands that are visible in the console. The GUI however, only has the most common statistical methods defined. For more advanced statistical methods, commands need to be entered into the console manually. Frequently, a series of commands is required to obtain the required output. To enter multiple commands sequentially can be rather tedious. Furthermore, if a mistake is made, you will need to start again!

Generally, it is preferable to save the commands as a script file (a text file of sequential R commands) that can be opened in R. A new script file can be created in JGR by selecting the ‘New Script File’ icon form the menu bar: newA script can be opened by selecting ‘Open Script File’ icon: openand saved by selecting the ‘Save Script File’ icon:save

This page contains useful scripts that are used in this book.

To use a script, select the appropriate hyperlink below. This should open a new browser window that contains the script (code). Select ALL (Ctrl-A) the text and copy it to the clipboard (Ctrl-C).

Create a new script in JGR:newand paste (Ctrl-V) the contents of the clipboard into the script editor.

Save the file saveand give it an appropriate name in your preferred location.

To run the script; select all the text in the script editor (Ctrl-A), copy it to the clipboard (Ctrl-C), paste it into the console (Ctrl-V) and hit return.

Functions:

Mode (calculate the mode of a variable)

BAplot (create Bland-Altman 1 plots)

ba_ggplot (create Bland-Altman 1 plots with ggplot)

nhs_number_validation (check if an NHS number is valid)

Dot Plot Data Function for comparison of genetic sequences. Returns a matrix with the first sequence as rows and second sequence as columns. Adapted from seqinr package 2.

KMatRisk (create Kaplan-Meier survival curves with numbers at risk)

DeducerSurvival (menu to create Kaplan-Meier survival curves)

It is however preferable to install the DeducerSurvival 3 function as a package from the CRAN website:

DeducerSurvival – CRAN

cool survival function

For this function, the RcmdrPlugin.KMggplot2, survival and ggfortify packages 4;5;6 should be installed, but there is no need to load the packages with the library command.

Export2Office (function by Tom Wenseleers that exports the active plot window directly to a Microsoft Powerpoint PPTX slide or a Microsoft Word DOCX file). The library ReporteRs 7 and it dependencies should be installed.

install.packages("ReporteRs")
install.packages("ReporteRsjars")

Pairwise alignment of DNA Please refer to a little book of R for bioinformatics 8

Plots as PDF, JPG or TIF (example on how to create pdf, jpg and tif files from plots created)

To create an SVG file, install the package svglite 9. Open the library and create a new file for the plot in the default directory (called Rplots.svg):

library(svglite)
svglite()

Now populate this file with a plot and close the file:

ggplot()+geom_point(aes(x=c(1,2,3,4),y=c(4,3,2,1)))+theme_bw()
dev.off()

Group dates by year (example on how to group dates by year; use the data file dates.rda with this example)

Create a survival curve from dates; a data frame that contains the date of diagnosis and date of failure (example on how to convert dates to follow up time and how to create the censor variable; use survivaldates.rda with this example).

Revalue of map values (factors) with the plyr package 10 (for example month names from the first date of the month:

library(plyr)
month<-mapvalues(activity$ActivityMonth,from=c(’01/01/2014′,’01/02/2014′,’01/03/2014′,’01/04/2014′,’01/05/2014′,’01/06/2014′,’01/07/2014′,’01/08/2014′,’01/09/2014′,’01/10/2014′,’01/11/2014′,’01/12/2014′),to=c(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’,’Jul’,’Aug’,’Sep’,’Oct’,’Nov’,’Dec’))

Convert incorrectly formatted data into appropriately declared variables that allow subsequent analysis.

It is a common error to group variables incorrectly and create a separate variable (column) for each group. However, variables should be in columns and the group is a separate variable. An example is show here.

Open an SPSS .sav file in R

Method 1:

  • Enter a1<-file.choose() in the console and maneuver to the file.
  • Activate the ‘foreign’ library: library(foreign)
  • dataset1<-read.spss(a1, reencode=’utf-8′, to.data.frame=TRUE)

This may give an error like: re-encoding from utf-8 Warning message: In read.spss(a1, reencode = “utf-8″, to.data.frame = TRUE) : Unrecognized record type 7, subtype 18 encountered in system file

Method 2:

  • Install the package ‘memisc’ 11
  • Enter library(memisc)
  • Enter a2<-file.choose() in the console and maneuver to the file.
  • enter: dataset2<-as.data.frame(as.data.set(spss.system.file(a2)))

Solar Functions (Equation of Time)

convert_dd_dms

convert_dms_dd

equation_of_time

sun_declination

sun_rise_formula

Other functions

R Objects and Operators

Common R Functions

Helpful R Functions in Packages

Defining R Functions

Importing and Cleaning Data in R

1.
Bland JM, Altman DG. Statistical methods for assessing agreement between two methods of clinical measurement. Lancet. 1986 Feb 8;1(8476):307–10.
1.
Charif D, Clerc O, Frank C, Lobry JR, Necsulea A, Palmeira L, et al. seqinr [Internet]. 2017. (seqinr: Biological Sequences Retrieval and Analysis). Available from: https://CRAN.R-project.org/package=seqinr
1.
Ockendon M, Cool P. DeducerSurvival: add survival dialogue to Deducer. R package version 0.1 [Internet]. 2012. Available from: http://CRAN.R-project.org/package=DeducerSurvival
1.
Horikoshi M, Tang Y. ggfortify: Data Visualization Tools for Statistical Analysis Results [Internet]. 2017. Available from: https://CRAN.R-project.org/package=ggfortify
1.
Sou T, Nagashima K. RcmdrPlugin.KMggplot2: R Commander Plug-in for Data Visualization with “ggplot2” [Internet]. 2016. Available from: https://CRAN.R-project.org/package=RcmdrPlugin.KMggplot2
1.
Therneau T. Survival: a package for survival analysis in R. R package version 2.37. 2014.
1.
Gohel D. ReporteRs: Microsoft Word, Microsoft PowerPoint and HTML Documents Generation [Internet]. 2016. Available from: https://cran.r-project.org/web/packages/ReporteRs/index.html
1.
Coghlan A. Welcome to a Little Book of R for Bioinformatics! [Internet]. Available from: http://a-little-book-of-r-for-bioinformatics.readthedocs.io/en/latest/
1.
Wickham H, Luciani T, Decorde M, Lise V, Plate T, Gohel D, et al. svglite [Internet]. 2015. (A SVG Graphics Device). Available from: https://cran.r-project.org/web/packages/svglite/index.html
1.
Wickham H. plyr [Internet]. 2015. Available from: https://cran.r-project.org/web/packages/plyr/index.html
1.
Elff M, Lawrence CN, Atkins D, Morgan JW, Zeileis A. memisc: Tools for Management of Survey Data and the Presentation of Analysis Results [Internet]. 2016. Available from: https://cran.r-project.org/web/packages/memisc/index.html