{"id":24,"date":"2015-04-28T10:41:20","date_gmt":"2015-04-28T09:41:20","guid":{"rendered":"http:\/\/pcool.dyndns.org:8080\/statsbook\/?page_id=24"},"modified":"2025-06-22T08:36:13","modified_gmt":"2025-06-22T07:36:13","slug":"functions","status":"publish","type":"page","link":"https:\/\/pcool.dyndns.org\/index.php\/functions\/","title":{"rendered":"Functions"},"content":{"rendered":"\n<p>When using the R, 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!<\/p>\n\n\n\n<p>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 RStudio by selecting File -&gt; New File -&gt; R Script form the menu bar.<\/p>\n\n\n\n<p>A script can be opened by selecting File -&gt; Open File and then manoeuvre to the file location.<\/p>\n\n\n\n<p>This page contains some useful scripts that are used in this book.<\/p>\n\n\n\n<p>To use a script, select the appropriate hyperlink below. This should open a new browser window that contains the script (code). Select <strong>ALL<\/strong> (Ctrl-A) the text and copy it to the clipboard (Ctrl-C).<\/p>\n\n\n\n<p>Create a new script in R (File -&gt; New File -&gt; R Script) and paste (Ctrl-V) the contents of the clipboard into the script editor.<\/p>\n\n\n\n<p>Save the file and give it an appropriate name in your preferred location.<\/p>\n\n\n\n<p><strong>To run the script<\/strong>; 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.<\/p>\n\n\n\n<p><strong>Functions:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/mode.txt\" target=\"_blank\" rel=\"noopener\">Mode<\/a> (calculate the mode of a variable)<\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/BAplot.txt\" target=\"_blank\" rel=\"noopener\">BAplot<\/a> (create Bland-Altman<sup class='sup-ref-note' id='note-zotero-ref-p24-r1-o1'><a class='sup-ref-note' href='#zotero-ref-p24-r1'>1<\/a><\/sup> plots)<\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/ba_ggplot.txt\" target=\"_blank\" rel=\"noopener\">ba_ggplot<\/a> (create Bland-Altman<sup class='sup-ref-note' id='note-zotero-ref-p24-r2-o1'><a class='sup-ref-note' href='#zotero-ref-p24-r2'>2<\/a><\/sup> plots with ggplot)<\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/nhs_number_validation.txt\" target=\"_blank\" rel=\"noopener\">nhs_number_validation<\/a>&nbsp;(check if an NHS number is valid)<\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/dot_plot_data.txt\" target=\"_blank\" rel=\"noopener\">Dot Plot Data<\/a> Function for comparison of genetic sequences. Returns a matrix with the first sequence as rows and second sequence as columns. Adapted from seqinr package<sup class='sup-ref-note' id='note-zotero-ref-p24-r3-o1'><a class='sup-ref-note' href='#zotero-ref-p24-r3'>3<\/a><\/sup>.<\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/kmatrisk.txt\" target=\"_blank\" rel=\"noopener\">KMatRisk<\/a> (create Kaplan-Meier survival curves with numbers at risk)<\/p>\n\n\n\n<p class=\"p1\"><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/cool_survival.txt\" target=\"_blank\" rel=\"noopener\">cool survival function<\/a><\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--1\"><em>For this <a href=\"https:\/\/pcool.dyndns.org\/index.php\/survival-plot\/\">function<\/a>, the RcmdrPlugin.KMggplot2, survival and ggfortify packages<sup class='sup-ref-note' id='note-zotero-ref-p24-r4-o1'><a class='sup-ref-note' href='#zotero-ref-p24-r4'>4<\/a><\/sup><sup class='sup-ref-note' id='note-zotero-ref-p24-r5-o1'><a class='sup-ref-note' href='#zotero-ref-p24-r5'>5<\/a><\/sup><sup class='sup-ref-note' id='note-zotero-ref-p24-r6-o1'><a class='sup-ref-note' href='#zotero-ref-p24-r6'>6<\/a><\/sup> should be installed, but there is no need to load the packages with the library command.<\/em><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/export2office.txt\" target=\"_blank\" rel=\"noopener\">Export2Office<\/a> (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<sup class='sup-ref-note' id='note-zotero-ref-p24-r7-o1'><a class='sup-ref-note' href='#zotero-ref-p24-r7'>7<\/a><\/sup> and it dependencies should be <a href=\"https:\/\/pcool.dyndns.org\/index.php\/packages\/\">installed<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">install.packages(\"ReporteRs\")<\/span><\/em>\n<em><span style=\"color: #ff0000;\">install.packages(\"ReporteRsjars\")<\/span><\/em><\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/pairwise_alignment.txt\" target=\"_blank\" rel=\"noopener\">Pairwise alignment of DNA<\/a> Please refer to a little book of R for bioinformatics <sup class='sup-ref-note' id='note-zotero-ref-p24-r8-o1'><a class='sup-ref-note' href='#zotero-ref-p24-r8'>8<\/a><\/sup><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/plots.txt\" target=\"_blank\" rel=\"noopener\">Plots as PDF, JPG or TIF <\/a>(example on how to create pdf, jpg and tif files from plots created)<\/p>\n\n\n\n<p>To create an SVG file, install the package svglite<sup class='sup-ref-note' id='note-zotero-ref-p24-r9-o1'><a class='sup-ref-note' href='#zotero-ref-p24-r9'>9<\/a><\/sup>. Open the library and create a new file for the plot in the default directory (called Rplots.svg):<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">library(svglite)<\/span><\/em>\n<em><span style=\"color: #ff0000;\">svglite()<\/span><\/em><\/code><\/pre>\n\n\n\n<p>Now populate this file with a plot and close the file:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">ggplot() + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_point(aes(x=c(1,2,3,4),y=c(4,3,2,1))) + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">theme_bw()<\/span><\/em>\n<em><span style=\"color: #ff0000;\">dev.off()<\/span><\/em><\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/groupdates.txt\" target=\"_blank\" rel=\"noopener\">Group dates by year <\/a>(example on how to group dates by year; use the data file <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/data_files\/dates.rda\" target=\"_blank\" rel=\"noopener\">dates.rda<\/a> with this example)<\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/survivaldates.txt\" target=\"_blank\" rel=\"noopener\">Create a survival curve from dates<\/a>; 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 <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/data_files\/survivaldates.rda\" target=\"_blank\" rel=\"noopener\">survivaldates.rda<\/a> with this example).<\/p>\n\n\n\n<p>Revalue of map values (factors) with the plyr package <sup class='sup-ref-note' id='note-zotero-ref-p24-r10-o1'><a class='sup-ref-note' href='#zotero-ref-p24-r10'>10<\/a><\/sup> (for example month names from the first date of the month:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>library(plyr)<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>month&lt;-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'))<\/em><\/span><\/code><\/pre>\n\n\n\n<p><strong>Convert incorrectly formatted data into appropriately declared variables that allow subsequent analysis.<\/strong><\/p>\n\n\n\n<p>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 <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/ConvertData.txt\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n\n\n\n<p><strong>Open an SPSS .sav file in R<\/strong><\/p>\n\n\n\n<p><strong>Method 1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Enter <span style=\"color: #ff0000;\"><em>a1&lt;-file.choose()<\/em><\/span> in the console and maneuver to the file.<\/li>\n\n\n\n<li>Activate the \u2018foreign\u2019 library: <span style=\"color: #ff0000;\"><em>library(foreign)<\/em><\/span><\/li>\n\n\n\n<li><span style=\"color: #ff0000;\"><em>dataset1&lt;-read.spss(a1, reencode=&#8217;utf-8&#8242;, to.data.frame=TRUE)<\/em><\/span><\/li>\n<\/ul>\n\n\n\n<p>This may give an error like: <em>re-encoding from utf-8 Warning message: In read.spss(a1, reencode = &#8220;utf-8&#8221;, to.data.frame = TRUE) : Unrecognized record type 7, subtype 18 encountered in system file<\/em><\/p>\n\n\n\n<p><strong>Method 2:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Install the package \u2018memisc\u2019<sup class='sup-ref-note' id='note-zotero-ref-p24-r11-o1'><a class='sup-ref-note' href='#zotero-ref-p24-r11'>11<\/a><\/sup><\/li>\n\n\n\n<li>Enter <span style=\"color: #ff0000;\"><em>library(memisc)<\/em><\/span><\/li>\n\n\n\n<li>Enter<em> <span style=\"color: #ff0000;\">a2&lt;-file.choose()<\/span><\/em> in the console and maneuver to the file.<\/li>\n\n\n\n<li>enter: <span style=\"color: #ff0000;\"><em>dataset2&lt;-as.data.frame(as.data.set(spss.system.file(a2)))<\/em><\/span><\/li>\n<\/ul>\n\n\n\n<p><strong>Solar Functions (<a href=\"https:\/\/pcool.dyndns.org\/index.php\/equation-of-time\/\">Equation of Time<\/a>)<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/convert_dd_dms.txt\" target=\"_blank\" rel=\"noopener\">convert_dd_dms<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/convert_dms_dd.txt\" target=\"_blank\" rel=\"noopener\">convert_dms_dd<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/equation_of_time.txt\" target=\"_blank\" rel=\"noopener\">equation_of_time<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/sun_declination.txt\" target=\"_blank\" rel=\"noopener\">sun_declination<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/sun_rise_formula.txt\" target=\"_blank\" rel=\"noopener\">sun_rise_formula<\/a><\/p>\n\n\n\n<p><strong>Other functions<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org\/index.php\/objects-and-operators\/\">R Objects and Operators<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org\/index.php\/common-r-functions\/\">Common R Functions<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org\/index.php\/helpful-r-functions-in-packages\/\">Helpful R Functions in Packages<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org\/index.php\/advanced-concepts\/\">Defining R Functions<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org\/index.php\/cleaning-data-in-r\/\">Importing and Cleaning Data in R<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using the R, 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-24","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":12,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/24\/revisions"}],"predecessor-version":[{"id":4256,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/24\/revisions\/4256"}],"wp:attachment":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/media?parent=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}