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).
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.
Mode (calculate the mode of a variable)
nhs_number_validation (check if an NHS number is valid)
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:
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.
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):
Now populate this file with a plot and close the file:
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:
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
- 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
- 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)