{"id":74,"date":"2015-08-25T17:20:50","date_gmt":"2015-08-25T15:20:50","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/?p=74"},"modified":"2015-08-25T18:32:36","modified_gmt":"2015-08-25T16:32:36","slug":"state-matrix-to-presence-absence-matrix","status":"publish","type":"post","link":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/2015\/08\/25\/state-matrix-to-presence-absence-matrix\/","title":{"rendered":"State-matrix to presence-absence-matrix"},"content":{"rendered":"<p><strong>A quick R example<\/strong><\/p>\n<p>Today, I needed to convert a series of state matrices into presence-absence matrices. In order to automate this conversion, I wrote the following R code. (The initiated will recognize the output as a species-range matrix.)<\/p>\n<p><em>1.a. Generate example input<\/em><\/p>\n<p style=\"padding-left: 30px\"><code>m = matrix(data=c(\"A\",\"B\",\"C\"), nrow=3, ncol=1)<br \/>\nrownames(m) = c(\"t1\", \"t2\", \"t3\")<br \/>\nm<\/code><br \/>\n<samp>[,1]<br \/>\nt1 &#8220;A&#8221;<br \/>\nt2 &#8220;B&#8221;<br \/>\nt3 &#8220;C&#8221;<\/samp><\/p>\n<p><em>1.b. Alternatively, one could load a table from file<\/em><\/p>\n<p style=\"padding-left: 30px\"><code>t = as.matrix(read.csv(\"~\/test.csv\", header=FALSE))<br \/>\nm = as.matrix(t[,2])<br \/>\nrownames(m) = t[,1]<br \/>\n<\/code><\/p>\n<p><em>2. Generate the presence-absence matrix<\/em><\/p>\n<p style=\"padding-left: 30px\"><code>l = length(unique(m[,1]))<br \/>\nm = cbind(m, matrix(rep(0,l), nrow=nrow(m), ncol=l))<br \/>\ncolnames(m)=c(NA,c(unique(m[,1])))<br \/>\nfor (i in c(unique(m[,1]))) {m[which(m[,1] %in% i), i]=1}<br \/>\nm = m[,-1]<br \/>\nm<\/code><br \/>\n<samp> A B C<br \/>\nt1 &#8220;1&#8221; &#8220;0&#8221; &#8220;0&#8221;<br \/>\nt2 &#8220;0&#8221; &#8220;1&#8221; &#8220;0&#8221;<br \/>\nt3 &#8220;0&#8221; &#8220;0&#8221; &#8220;1&#8221;<\/samp><\/p>\n<p><em>EDIT:<\/em> If any of the entries in the state matrix were to contain a <strong>combination<\/strong> of states (e.g., &#8220;A and B&#8221;), the following code should be used at step #2:<\/p>\n<p style=\"padding-left: 30px\"><code>tmp = unique(m[,1])<br \/>\nnew = tmp[-which(tmp==tmp[grepl(\" and \", tmp)])]<br \/>\naddon = unlist(strsplit(tmp[grepl(\" and \", tmp)], ' and '))<br \/>\nul = unique(c(new,addon))<br \/>\nl = length(ul)<br \/>\nm = cbind(m, matrix(rep(0,l), nrow=nrow(m), ncol=l))<br \/>\ncolnames(m)=c(NA,ul)<br \/>\nfor (i in ul) {m[which(grepl(i, m[,1])), i]=1}<br \/>\nm = m[,-1]<br \/>\n<\/code><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A quick R example Today, I needed to convert a series of state matrices into presence-absence matrices. In order to automate this conversion, I wrote the following R code. (The initiated will recognize the output as a species-range matrix.) 1.a. Generate example input m = matrix(data=c(&#8220;A&#8221;,&#8221;B&#8221;,&#8221;C&#8221;), nrow=3, ncol=1) rownames(m) = c(&#8220;t1&#8221;, &#8220;t2&#8221;, &#8220;t3&#8221;) m [,1] [&hellip;]<\/p>\n","protected":false},"author":2306,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57598],"tags":[],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-bioinformatics"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/users\/2306"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":15,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/posts\/74\/revisions\/89"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}