<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scienceoss.com &#187; dataframe</title>
	<atom:link href="http://scienceoss.com/tags/dataframe/feed/" rel="self" type="application/rss+xml" />
	<link>http://scienceoss.com</link>
	<description>useful tidbits for using open source software in science</description>
	<lastBuildDate>Wed, 26 May 2010 03:34:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Restructure or reformat dataframes in R with melt</title>
		<link>http://scienceoss.com/restructure-or-reformat-dataframes-in-r-with-melt/</link>
		<comments>http://scienceoss.com/restructure-or-reformat-dataframes-in-r-with-melt/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 22:06:17 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[data management]]></category>
		<category><![CDATA[dataframe]]></category>
		<category><![CDATA[melt]]></category>

		<guid isPermaLink="false">http://scienceoss.com/restructure-or-reformat-dataframes-in-r-with-melt/</guid>
		<description><![CDATA[The basic idea is to take an R dataframe like this one containing abundance of three species at each site, and elevation at each site site sp1 sp2 sp3 elev a 3 4 9 100 b 1 8 10 210 c 4 8 15 165 and reorganize into something like this (perhaps so we can [...]]]></description>
			<content:encoded><![CDATA[<p>The basic idea is to take an R dataframe like this one containing abundance of three species at each site, and elevation at each site</p>
<pre class="prettyprint"><code class="code">site  sp1 sp2 sp3 elev
a      3   4   9   100
b      1   8   10  210
c      4   8   15  165
</code></pre>
<p>and reorganize into something like this (perhaps so we can do an ANOVA using species as a factor):</p>
<pre class="prettyprint"><code class="code">site  elev  sp  abundance
a     100  sp1  3
a     100  sp2  4
a     100  sp3  9
b     210  sp1  1
b     210  sp2  8
b     210  sp3  10
c     165  sp1  4
c     165  sp2  8
c     165  sp3  15</code></pre>
<p>Assuming the first dataframe above is called <span class="c">d</span>, the second dataframe can be obtained using the following code:</p>
<pre class="prettyprint"><code class="code">> library(ggplot2)
> m = melt(d, id=c('site','elev'))
</code></pre>
<p><span class="c">melt</span> works like this: You specify the ID variables, which are those variables that will REMAIN as dataframe variables.  Any others will be considered measured variables.  If it&#8217;s easier for your data, you can do it the other way: specify the measured variables and the others will be considered ID variables.</p>
<p>Melting results in two new variables, <span class="c">variable</span> and <span class="c">value</span>.  <span class="c">variable</span> contains the names of the original columns of the dataframe as factors, and <span class="c">value</span> contains the corresponding values.</p>
<h3>Another example</h3>
<p>Here&#8217;s another example using the built-in dataset, airquality.  First, unmelted:</p>
<pre class="prettyprint"><code class="code"># make all the variable names lowercase
names(airquality) <- tolower(names(airquality))
head(airquality)
  ozone solar.r wind temp month day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6
</code></pre>
<p>and melted:</p>
<pre class="prettyprint"><code class="code">> head(melt(airquality,id=c('month','day')))
  month day variable value
1     5   1    ozone    41
2     5   2    ozone    36
3     5   3    ozone    12
4     5   4    ozone    18
5     5   5    ozone    NA
6     5   6    ozone    28</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/restructure-or-reformat-dataframes-in-r-with-melt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Combine dataframes in R</title>
		<link>http://scienceoss.com/combine-dataframes-in-r/</link>
		<comments>http://scienceoss.com/combine-dataframes-in-r/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 15:15:11 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[dataframe]]></category>
		<category><![CDATA[merge]]></category>

		<guid isPermaLink="false">http://scienceoss.com/combine-dataframes-in-r/</guid>
		<description><![CDATA[Quick answer: use merge. You can also use rbind, but it will only be useful for simple cases. See http://www.statmethods.net/management/merging.html for more details.]]></description>
			<content:encoded><![CDATA[<p>Quick answer:  use <span class="c">merge</span>.  </p>
<p>You can also use <span class="c">rbind</span>, but it will only be useful for simple cases.  </p>
<p>See <a href="http://www.statmethods.net/management/merging.html">http://www.statmethods.net/management/merging.html</a> for more details.</p>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/combine-dataframes-in-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

