{"id":1689,"date":"2016-06-17T21:02:25","date_gmt":"2016-06-17T20:02:25","guid":{"rendered":"http:\/\/pcool.dyndns.org:8080\/statsbook\/?page_id=1689"},"modified":"2025-06-30T18:19:27","modified_gmt":"2025-06-30T17:19:27","slug":"advanced-concepts","status":"publish","type":"page","link":"https:\/\/pcool.dyndns.org\/index.php\/advanced-concepts\/","title":{"rendered":"Advanced Concepts"},"content":{"rendered":"\n<p>On this page, advanced concepts are discussed for the more experienced user. In particular, the manipulation of data and the assigning \/ arranging of new variables.<\/p>\n\n\n\n<p>To rearrange \/ group data, it is useful to know several R operators \/ functions.<\/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\/define-r-functions\/\">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><strong>R data manipulation functions examples:<\/strong><\/p>\n\n\n\n<p>To select a subset of data; for example select different observers (1 and 2) and create a new data frame with data of both observers in columns (the original data frame is called &#8216;results&#8217; with as variables &#8216;observer&#8217;, &#8216;rotation&#8217; and &#8216;outcome&#8217;):<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\">observer1 &lt;- subset(results,observer==1,select=c(outcome, rotation))<\/span>\n<span style=\"color: #ff0000;\"> names(observer1)&#091;names(observer1)==\"outcome\"]&lt;-'outcome1'<\/span>\n<span style=\"color: #ff0000;\"> names(observer1)&#091;names(observer1)==\"rotation\"]&lt;-'rotation1'<\/span>\n<span style=\"color: #ff0000;\"> observer2&lt;-subset(results,observer==2,select=c(outcome,rotation))<\/span>\n<span style=\"color: #ff0000;\"> names(observer2)&#091;names(observer2)==\"outcome\"]&lt;-'outcome2'<\/span>\n<span style=\"color: #ff0000;\"> names(observer2)&#091;names(observer2)==\"rotation\"]&lt;-'rotation2'<\/span>\n<span style=\"color: #ff0000;\"> observers&lt;-cbind(observer1,observer2)<\/span><\/code><\/pre>\n\n\n\n<p>Some of these functions \/ operators are used in the examples below.<\/p>\n\n\n\n<p><strong>Printing percentages and currency signs:<\/strong><\/p>\n\n\n\n<p>To print percentages, use the sprintf function from R. For help on formatting, type <span style=\"color: #ff0000;\">?sprintf<\/span> in the console. A full description is outside the scope of this page. However, the first argument of the sprintf function should be within quotation marks (&#8220;). This argument starts with a % sign (to show a variable is coming), is followed by a full stop (to indicate the decimal point), followed by the number of decimal characters followed by an f (for floating point variable) and finally followed %% (the first percent sign is an &#8216;escape&#8217; character as it would otherwise indicate a variable). The second argument of the function is the variable it should be applied to. Therefore, to print a percentage (%) sign behind a number:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\">a &lt;- c(1,2,3,4,5,6,7,8,9)<\/span>\n<span style=\"color: #ff0000;\">a<\/span>\n<span style=\"color: #0000ff;\">&#091;1] 1 2 3 4 5 6 7 8 9<\/span>\n<span style=\"color: #ff0000;\">b &lt;- sprintf(\"%.0f%%\",a)<\/span>\n<span style=\"color: #ff0000;\">b<\/span>\n<span style=\"color: #0000ff;\">&#091;1] \"1%\" \"2%\" \"3%\" \"4%\" \"5%\" \"6%\" \"7%\" \"8%\" \"9%\"<\/span><\/code><\/pre>\n\n\n\n<p>Similarly, to print a % sign with two decimal places:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\">c &lt;- sprintf(\"%.2f%%\", a)<\/span>\n<span style=\"color: #ff0000;\">c<\/span>\n<span style=\"color: #0000ff;\">&#091;1] \"1.00%\" \"2.00%\" \"3.00%\" \"4.00%\" \"5.00%\" \"6.00%\" \"7.00%\" \"8.00%\" \"9.00%\"<\/span><\/code><\/pre>\n\n\n\n<p>Finally, to print a \u00a3 sign (for example):<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\">d &lt;- sprintf(\"\u00a3 %.2f\", a)<\/span>\n<span style=\"color: #ff0000;\">d<\/span>\n<span style=\"color: #0000ff;\">&#091;1] \"\u00a3 1.00\" \"\u00a3 2.00\" \"\u00a3 3.00\" \"\u00a3 4.00\" \"\u00a3 5.00\" \"\u00a3 6.00\" \"\u00a3 7.00\" \"\u00a3 8.00\" \"\u00a3 9.00\"<\/span><\/code><\/pre>\n\n\n\n<p><strong>Manipulating dates:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/survivaldates.txt\" target=\"_blank\" rel=\"noreferrer 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=\"noreferrer 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-p1689-r1-o1'><a class='sup-ref-note' href='#zotero-ref-p1689-r1'>1<\/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>Calculating the &#8216;day number&#8217; of a date:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">datum&lt;-as.Date(as.character('31\/12\/2000'),'%d\/%m\/%Y')<\/span><\/em>\n<em><span style=\"color: #0000ff;\">datum<\/span><\/em>\n<em><span style=\"color: #0000ff;\">&#091;1] \"2000-12-31\"<\/span><\/em>\n# the date is converted to <a href=\"https:\/\/www.iso.org\/iso-8601-date-and-time-format.html\" data-type=\"link\" data-id=\"https:\/\/www.iso.org\/iso-8601-date-and-time-format.html\" target=\"_blank\" rel=\"noreferrer noopener\">ISO format<\/a> as recommended\n<em><span style=\"color: #ff0000;\">format(datum,format='%j')<\/span><\/em>\n<em><span style=\"color: #0000ff;\">&#091;1] \"366\"<\/span><\/em><\/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=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p><strong>&nbsp;Examples:<\/strong><\/p>\n\n\n\n<p>Please note the ggplot2 package<sup class='sup-ref-note' id='note-zotero-ref-p1689-r2-o1'><a class='sup-ref-note' href='#zotero-ref-p1689-r2'>2<\/a><\/sup> will have to be loaded.<\/p>\n\n\n\n<p>To create a faceted bar chart, load the <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/data_files\/Diag.rda\" target=\"_blank\" rel=\"noreferrer noopener\">Diag.rda<\/a> data frame into JGR and run the <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/Diag.txt\" target=\"_blank\" rel=\"noreferrer noopener\">create faceted bar chart <\/a>function. This will create the following plot:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"627\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Diag-1024x627.png\" alt=\"\" class=\"wp-image-3104\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Diag-1024x627.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Diag-300x184.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Diag-768x470.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Diag-1536x941.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Diag-2048x1254.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This example shows how to regroup data and create a plot on defined criteria.<\/p>\n\n\n\n<p>To create a stacked and faceted bar plot, load the <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/data_files\/SpecGroup.rda\" target=\"_blank\" rel=\"noreferrer noopener\">SpecGroup.rda <\/a>data frame into JGR and run the s<a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/SpecGroup.txt\" target=\"_blank\" rel=\"noreferrer noopener\">tacked faceted bar plot 1 <\/a>function. This will create the following plot:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"747\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/SpecGroup-1024x747.png\" alt=\"\" class=\"wp-image-3648\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/SpecGroup-1024x747.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/SpecGroup-300x219.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/SpecGroup-768x560.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/SpecGroup-1536x1121.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/SpecGroup-2048x1494.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>To create a stacked and faceted bar plot where the axes are &#8216;free&#8217; and only labels that are greater than 1 are displayed; load the <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/data_files\/TNMStage.rda\" data-type=\"link\" data-id=\"https:\/\/pcool.dyndns.org:\/wp-content\/data_files\/TNMstage.rda\" target=\"_blank\" rel=\"noreferrer noopener\">TNMstage.rda<\/a> data frame into R and run the <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/TNMstage.txt\" data-type=\"link\" data-id=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/TNMstage.txt\" target=\"_blank\" rel=\"noreferrer noopener\">stacked faceted bar plot 2<\/a> function. This will create the following plot:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"676\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/TNMstage-1024x676.png\" alt=\"\" class=\"wp-image-3828\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/TNMstage-1024x676.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/TNMstage-300x198.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/TNMstage-768x507.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/TNMstage-1536x1014.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/TNMstage-2048x1352.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>On this page, advanced concepts are discussed for the more experienced user. In particular, the manipulation of data and the assigning \/ arranging of new variables. To rearrange \/ group data, it is useful to know several R operators \/ functions. R Objects and Operators Common R Functions Helpful R Functions in Packages Defining R [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-1689","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/1689","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=1689"}],"version-history":[{"count":6,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/1689\/revisions"}],"predecessor-version":[{"id":4629,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/1689\/revisions\/4629"}],"wp:attachment":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/media?parent=1689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}