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: A script can be opened by selecting ‘Open Script File’ icon: and saved by selecting the ‘Save Script File’ icon:
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:and paste (Ctrl-V) the contents of the clipboard into the script editor.
Save the file and 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:
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)
Other functions
Helpful R Functions in Packages
Importing and Cleaning Data in R