{"id":936,"date":"2012-01-23T17:03:07","date_gmt":"2012-01-23T22:03:07","guid":{"rendered":"http:\/\/anthony.darrouzet-nardi.net\/scienceblog\/?p=936"},"modified":"2012-01-23T17:03:20","modified_gmt":"2012-01-23T22:03:20","slug":"r-trick-sorting-and-keeping-track-of-many-response-variables","status":"publish","type":"post","link":"http:\/\/scienceblog.darrouzet-nardi.net\/?p=936","title":{"rendered":"R tip: Sorting and keeping track of many response variables"},"content":{"rendered":"<p>When doing the same analyses on many different response variables, it can be hard to keep track of the variables and have R print graphs and other output with the response variables of choice in the desired order. Here&#8217;s an example of the column names from a multivariate data set I&#8217;m working with:<\/p>\n<p><code>&gt; names(nts.summ)<br \/>\n[1] \"date\" \"Season\" \"Plot\" \"DOC\" \"MBC\" \"DON\" \"MB.N\"<br \/>\n[8] \"NH4\" \"NO3\" \"moisture\" \"NAG\" \"CBH\" \"AG\" \"BG\"<br \/>\n[15] \"LAP\" \"BXYL\" \"PHOS\" \"PHENOX\" \"NetPerox\" \"UREASE\" \"logNH4\"<br \/>\n[22] \"logNO3\" \"logLAP\" \"logPHENOX\" \"logNetPerox\" \"microbialCN\" \"extractCN\" \"tempmax\"<br \/>\n[29] \"tempmin\" \"tempmean\"<\/code><\/p>\n<p>To make these easier to work with, I keep a character vector with the response variable names in the desired order (<code>variables<\/code>) and then create a reference vector <code>(y)<\/code> with a short <code>for<\/code> loop:<\/p>\n<p><code>variables = c(\"DOC\", \"DON\", \"extractCN\", \"MBC\", \"MB.N\", \"microbialCN\",\"NH4\", \"NO3\", \"NAG\", \"CBH\", \"AG\", \"BG\", \"BXYL\",\u00a0\"PHOS\", \"LAP\", \"PHENOX\", \"NetPerox\", \"UREASE\", \"moisture\", \"tempmean\")<br \/>\ny = NA; for(x in 1:length(variables)) {<br \/>\n&nbsp;&nbsp;y[x] &lt;- which(names(nts.summ)==variables[x]) }<\/code><\/p>\n<p><code>&gt; y<br \/>\n[1] 4 6 27 5 7 26 8 9 11 12 13 14 16 17 15 18 19 20 10 30<br \/>\n<\/code><br \/>\nNow if I want to do a scatterplot matrix or, say, print the median for each variable, I can use the reference vector <code>y.<\/code><\/p>\n<p><code>splom(nts.summ[y])<br \/>\nfor (x in y) print(median(nts[,x],na.rm=T))<\/code><\/p>\n<p>The reference vector <code>y<\/code> can easily be modified as the analyses evolve. For example, if I decide to switch to a log-transformed version of a few variables, I just change their names in the code for <code>variables<\/code>, create a new <code>y<\/code>, and re-run.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When doing the same analyses on many different response variables, it can be hard to keep track of the variables and have R print graphs and other output with the response variables of choice in the desired order. Here&#8217;s an example of the column names from a multivariate data set I&#8217;m working with: &gt; names(nts.summ) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/scienceblog.darrouzet-nardi.net\/index.php?rest_route=\/wp\/v2\/posts\/936"}],"collection":[{"href":"http:\/\/scienceblog.darrouzet-nardi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/scienceblog.darrouzet-nardi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/scienceblog.darrouzet-nardi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/scienceblog.darrouzet-nardi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=936"}],"version-history":[{"count":30,"href":"http:\/\/scienceblog.darrouzet-nardi.net\/index.php?rest_route=\/wp\/v2\/posts\/936\/revisions"}],"predecessor-version":[{"id":966,"href":"http:\/\/scienceblog.darrouzet-nardi.net\/index.php?rest_route=\/wp\/v2\/posts\/936\/revisions\/966"}],"wp:attachment":[{"href":"http:\/\/scienceblog.darrouzet-nardi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/scienceblog.darrouzet-nardi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=936"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/scienceblog.darrouzet-nardi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}