<?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>Stochastic Nonsense &#187; R Tips</title>
	<atom:link href="http://blog.earlh.com/index.php/tag/r-tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.earlh.com</link>
	<description></description>
	<lastBuildDate>Mon, 19 Sep 2011 03:30:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Querying Databases From R on a Mac</title>
		<link>http://blog.earlh.com/index.php/2010/01/querying-databases-from-r-on-a-mac/</link>
		<comments>http://blog.earlh.com/index.php/2010/01/querying-databases-from-r-on-a-mac/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 00:28:08 +0000</pubDate>
		<dc:creator>earl</dc:creator>
				<category><![CDATA[Data Munging]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[R Tip]]></category>
		<category><![CDATA[greenplum]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[R and Databases]]></category>
		<category><![CDATA[R Tips]]></category>

		<guid isPermaLink="false">http://blog.earlh.com/?p=520</guid>
		<description><![CDATA[I use a mac, currently running OS 10.6 / Snow Leopard, and I&#8217;d like to query our greenplum / postgres database from R. This used to work with R 2.9, but I unfortunately had to upgrade R, and R 2.10 &#8230; <a href="http://blog.earlh.com/index.php/2010/01/querying-databases-from-r-on-a-mac/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I use a mac, currently running OS 10.6 / Snow Leopard, and I&#8217;d like to query our greenplum / postgres database from R.  This used to work with R 2.9, but I unfortunately had to upgrade R, and R 2.10 on the mac is a 64 bit app.  So, I want to use either RODBC or RPostgreSQL packages under 64 bit R on a mac to query postgres / greenplum.</p>
<p>First, I tried just <a href="http://blog.earlh.com/index.php/2009/12/querying-postgres-or-greenplum-from-r-on-a-mac/">installing RPostgreSQL</a> as before.  Unfortunately, I started getting weird errors when I attempted to load the package:</p>
<p><code>
<pre  class="brush:text;">
>library('RPostgreSQL')
Loading required package: DBI
Error in dyn.load(file, DLLpath = DLLpath, ...) :
  unable to load shared library '/Library/Frameworks/R.framework/Resources/library/RPostgreSQL/libs/x86_64/RPostgreSQL.so':
  dlopen(/Library/Frameworks/R.framework/Resources/library/RPostgreSQL/libs/x86_64/RPostgreSQL.so, 6): Symbol not found: _PQbackendPID
  Referenced from: /Library/Frameworks/R.framework/Resources/library/RPostgreSQL/libs/x86_64/RPostgreSQL.so
  Expected in: flat namespace
 in /Library/Frameworks/R.framework/Resources/library/RPostgreSQL/libs/x86_64/RPostgreSQL.so
Error: package/namespace load failed for 'RPostgreSQL'
</pre>
<p></code></p>
<p>The key bit of the error message is the missing symbol: _PQbackendPID.  Some googling suggested this could be caused by mixing 32 and 64 bit libs.  I used file to check and yes, indeed, I had a 32 bit version of Postgres that was refusing to talk to a 64 bit version on R.  Suck.</p>
<p>In brief, the solution is to use ports to install postgres &#8212; in this case, postgres 8.4 as such:<br />
<code>
<pre class="brush:bash;">
sudo port install postgres84
</pre>
<p></code></p>
<p>you can use the file command to see what architecture your installed postgres is configured as:<br />
<code>
<pre  class="brush:bash;">
laptop:src earl$ file `echo $PG_CONFIG`
/opt/local/lib/postgresql84/bin/pg_config: Mach-O 64-bit executable x86_64
</pre>
<p></code></p>
<p>checking, my previous postgres 8.4 install, from the Postgres Plus prebuild package, produces<br />
<code>
<pre  class="brush:bash;">
file /Library/PostgresPlus/8.4SS/bin/pg_config
/Library/PostgresPlus/8.4SS/bin/pg_config: Mach-O universal binary with 2 architectures
/Library/PostgresPlus/8.4SS/bin/pg_config (for architecture ppc):	Mach-O executable ppc
/Library/PostgresPlus/8.4SS/bin/pg_config (for architecture i386):	Mach-O executable i386
</pre>
<p></code><br />
Notice the lack of any 64bit support.</p>
<p>Then open a terminal, set the PG_CONFIG environmental variable to point to the right location, then run R from the terminal and install the package.<br />
<code>
<pre class="brush:text;">
laptop: work earl$ export PG_CONFIG=/opt/local/lib/postgresql84/bin/pg_config

laptop: work earl$ R64
install.packages('RPostgreSQL', type='source')
</pre>
<p></code></p>
<p>If you have misconfigured the pg_config, this is the relevant bit of the compilation error message you will receive:<br />
<code>
<pre class="brush:text;">
checking for "/libpq-fe.h"... no
configure: error: File libpq-fe.h not in ; installation may be broken.
ERROR: configuration failed for package ‘RPostgreSQL’
* removing ‘/Library/Frameworks/R.framework/Versions/2.10/Resources/library/RPostgreSQL’
</code></pre>
<p>Otherwise, RPostgreSQL will compile and install.  Seriously, though, there *must* be a better way of distributing software on macs.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.earlh.com/index.php/2010/01/querying-databases-from-r-on-a-mac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Querying Postgres or Greenplum from R on a Mac</title>
		<link>http://blog.earlh.com/index.php/2009/12/querying-postgres-or-greenplum-from-r-on-a-mac/</link>
		<comments>http://blog.earlh.com/index.php/2009/12/querying-postgres-or-greenplum-from-r-on-a-mac/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 16:00:50 +0000</pubDate>
		<dc:creator>earl</dc:creator>
				<category><![CDATA[Data Munging]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[R Tip]]></category>
		<category><![CDATA[greenplum]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[R and Databases]]></category>
		<category><![CDATA[R Tips]]></category>

		<guid isPermaLink="false">http://blog.earlh.com/?p=501</guid>
		<description><![CDATA[So, I&#8217;m using snow leopard, and I want to query our postgres / greenplum database. First things first: I&#8217;m familiar with the RODBC package on CRAN. This installs fine, since it&#8217;s a binary package. I also installed the ODBC Administrator &#8230; <a href="http://blog.earlh.com/index.php/2009/12/querying-postgres-or-greenplum-from-r-on-a-mac/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So, I&#8217;m using snow leopard, and I want to query our postgres / greenplum database.</p>
<p>First things first: I&#8217;m familiar with the <a href="http://cran.r-project.org/web/packages/RODBC/index.html">RODBC</a> package on CRAN.  This installs fine, since it&#8217;s a binary package.  I also installed the ODBC Administrator app that you have to download from apple <a href="http://support.apple.com/downloads/ODBC_Administrator_Tool_for_Mac_OS_X"> here </a>.  Now all I need is the postgres ODBC driver, which is harder to get your hands on than you&#8217;d think.  I first installed postgres84 via ports, but that didn&#8217;t seem to include the ODBC driver.  I then installed the full postgres84 package in a pre-packaged distro from <a href="http://www.enterprisedb.com/products/pgdownload.do#osx"> EnterpriseDB</a>.  This required rebooting my mac and then manually disabling postgres db &#8212; since I only want the odbc drivers &#8212; by removing the obvious files from /Library/LaunchDaemons.  Then&#8230; no love.  I started ODBC Administrator, selected a System DSN, chose the psqlODBC driver, and then ended up with a screen that had no prompts and just a bunch of key / value pairs with no suggestions as to what might be required &#8212; typically some variation of host, hostname, user, username, etc.  Unfortunately, clicking on the key field in the rows doesn&#8217;t allow me to edit them;  Hitting enter allows me to modify the key, but hell if I know how to modify the value.</p>
<p>So my next attempt was installing the <a href="http://cran.r-project.org/web/packages/RPostgreSQL/index.html">RPostgreSQL</a> package from CRAN.<br />
<code>
<pre class="brush:text;">
install.packages('RPostgreSQL')
</pre>
<p></code><br />
fails, as by default R will only grab binary packages and this is a source package.  You will have to do this:<br />
<code>
<pre class="brush:text;">
install.packages('RPostgreSQL', type='source')
</pre>
<p></code></p>
<p>This, of course, then fails to build, complaining that it can&#8217;t find libpq-fe.h.  Awesome.</p>
<p>If you look hard enough, the missing header file should be wherever you installed postgres.  Either in /opt/local/something if you used ports to install postgres, or in /Library/PostgresPlus/8.4SS if you installed the binary distribution as I did.  Inside that directory lives an include directory which has our .h file.  Setting PG_INCDIR to that path &#8212; eg<br />
<code>
<pre class="brush:bash;">
export PG_INCDIR="/Library/PostgresPlus/8.4SS/include"
</pre>
<p></code></p>
<p>then running R from that shell now gets me far enough that when you rerun install.packages from R you get a complaint about a missing lib:<br />
<code>
<pre class="brush:text;">
> install.packages('RPostgreSQL')
--- Please select a CRAN mirror for use in this session ---
Loading Tcl/Tk interface ... done
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
  package ‘RPostgreSQL’ is not available
> ? install.packages
> install.packages('RPostgreSQL', type='source')
also installing the dependency ‘DBI’

trying URL 'http://cran.stat.ucla.edu/src/contrib/DBI_0.2-5.tar.gz'
Content type 'application/x-tar' length 308395 bytes (301 Kb)
opened URL
==================================================
downloaded 301 Kb

trying URL 'http://cran.stat.ucla.edu/src/contrib/RPostgreSQL_0.1-6.tar.gz'
Content type 'application/x-tar' length 141399 bytes (138 Kb)
opened URL
==================================================
downloaded 138 Kb

* Installing *source* package ‘DBI’ ...
** R
** inst
** preparing package for lazy loading
Creating a new generic function for "summary" in "DBI"
** help
*** installing help indices
 >>> Building/Updating help pages for package 'DBI'
     Formats: text html latex example
  DBI-internal                      text    html    latex
  DBIConnection-class               text    html    latex   example
  DBIDriver-class                   text    html    latex   example
  DBIObject-class                   text    html    latex   example
  DBIResult-class                   text    html    latex   example
  dbCallProc                        text    html    latex
  dbCommit                          text    html    latex   example
  dbConnect                         text    html    latex   example
  dbDataType                        text    html    latex   example
  dbDriver                          text    html    latex   example
  dbGetInfo                         text    html    latex   example
  dbListTables                      text    html    latex   example
  dbReadTable                       text    html    latex   example
  dbSendQuery                       text    html    latex   example
  dbSetDataMappings                 text    html    latex   example
  fetch                             text    html    latex   example
  make.db.names                     text    html    latex   example
  print.list.pairs                  text    html    latex   example
** building package indices ...
* DONE (DBI)
* Installing *source* package ‘RPostgreSQL’ ...
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for pg_config... no
configure: checking for PostgreSQL header files
checking for "/Library/PostgresPlus/8.4SS/include/libpq-fe.h"... yes
configure: creating ./config.status
config.status: creating src/Makevars
** libs
** arch - i386
gcc -arch i386 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/i386 -I/Library/PostgresPlus/8.4SS/include -I/usr/local/include    -fPIC  -g -O2 -c RS-DBI.c -o RS-DBI.o
gcc -arch i386 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/i386 -I/Library/PostgresPlus/8.4SS/include -I/usr/local/include    -fPIC  -g -O2 -c RS-PostgreSQL.c -o RS-PostgreSQL.o
gcc -arch i386 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names -mmacosx-version-min=10.4 -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -o RPostgreSQL.so RS-DBI.o RS-PostgreSQL.o -L -lpq -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: library not found for -lpq
collect2: ld returned 1 exit status
make: *** [RPostgreSQL.so] Error 1
ERROR: compilation failed for package ‘RPostgreSQL’
* Removing ‘/Library/Frameworks/R.framework/Versions/2.9/Resources/library/RPostgreSQL’

The downloaded packages are in
	‘/private/var/folders/-E/-E9MDL2qECqW8Ik4CfUX6U+++TM/-Tmp-/RtmpvTtehd/downloaded_packages’
Updating HTML index of packages in '.Library'
Warning message:
In install.packages("RPostgreSQL", type = "source") :
  installation of package 'RPostgreSQL' had non-zero exit status
</pre>
<p></code></p>
<p>Thanks to an email to the <a href="http://www.mail-archive.com/r-help@r-project.org/msg67209.html">R help list</a>, the answer is to tell gcc where to find pg_config, which somehow magically solves this.  eg:<br />
<code>
<pre class="brush:text;">
earl:bin $ export PG_CONFIG=/Library/PostgresPlus/8.4SS/bin/pg_config
earl:bin $ R

R version 2.9.2 (2009-08-24)
[...]
> install.packages('RPostgreSQL', type='source')
--- Please select a CRAN mirror for use in this session ---
[...]
checking for pg_config... /Library/PostgresPlus/8.4SS/bin/pg_config
checking for "/Library/PostgresPlus/8.4SS/include/libpq-fe.h"... yes
configure: creating ./config.status
config.status: creating src/Makevars
** libs
** arch - i386
gcc -arch i386 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/i386 -I/Library/PostgresPlus/8.4SS/include -I/usr/local/include    -fPIC  -g -O2 -c RS-DBI.c -o RS-DBI.o
gcc -arch i386 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/i386 -I/Library/PostgresPlus/8.4SS/include -I/usr/local/include    -fPIC  -g -O2 -c RS-PostgreSQL.c -o RS-PostgreSQL.o
gcc -arch i386 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names -mmacosx-version-min=10.4 -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -o RPostgreSQL.so RS-DBI.o RS-PostgreSQL.o -L/Library/PostgresPlus/8.4SS/lib -lpq -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
** R
** inst
** preparing package for lazy loading
Creating a new generic function for "format" in "RPostgreSQL"
Creating a new generic function for "print" in "RPostgreSQL"
** help
*** installing help indices
 >>> Building/Updating help pages for package 'RPostgreSQL'
     Formats: text html latex example
  PostgreSQL                        text    html    latex   example
  PostgreSQLConnection-class        text    html    latex   example
  PostgreSQLDriver-class            text    html    latex   example
  PostgreSQLObject-class            text    html    latex   example
  PostgreSQLResult-class            text    html    latex   example
  S4R                               text    html    latex   example
  dbApply-methods                   text    html    latex   example
  dbApply                           text    html    latex   example
  dbBuildTableDefinition            text    html    latex
  dbCallProc-methods                text    html    latex
  dbCommit-methods                  text    html    latex   example
  dbConnect-methods                 text    html    latex   example
  dbDataType-methods                text    html    latex   example
  dbDriver-methods                  text    html    latex   example
  dbGetInfo-methods                 text    html    latex   example
  dbListTables-methods              text    html    latex   example
  dbObjectId-class                  text    html    latex   example
  dbReadTable-methods               text    html    latex   example
  dbSendQuery-methods               text    html    latex   example
  dbSetDataMappings-methods         text    html    latex   example
  fetch-methods                     text    html    latex   example
  isIdCurrent                       text    html    latex   example
  make.db.names-methods             text    html    latex   example
  postgresqlDBApply                 text    html    latex   example
  postgresqlSupport                 text    html    latex
  safe.write                        text    html    latex   example
  summary-methods                   text    html    latex
** building package indices ...
* DONE (RPostgreSQL)

The downloaded packages are in
	‘/private/var/folders/-E/-E9MDL2qECqW8Ik4CfUX6U+++TM/-Tmp-/RtmpurzqTb/downloaded_packages’
Updating HTML index of packages in '.Library'
> library(RPostgreSQL)
Loading required package: DBI
</pre>
<p></code></p>
<p>You can now test this:<br />
<code>
<pre class="brush:text;">
> library('RPostgreSQL')
Loading required package: DBI
> drv <- dbDriver('PostgreSQL')
> drv
 PostgreSQLDriver:(1825)
> db <- dbConnect(drv, host='greenplum.ip', user='earl', dbname='db01')
> db
 PostgreSQLConnection:(1825,0)
> dbGetQuery(db, 'select 1')
  ?column?
1        1
>
>
>
> dbGetQuery(db, 'select count(*) from earl_fav_wd')
  count
1    34
>
</pre>
<p></code></p>
<p>Success!  I can query my greenplum db from R.  Also, I hate computers.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.earlh.com/index.php/2009/12/querying-postgres-or-greenplum-from-r-on-a-mac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Querying Databases in R, on Mac OS</title>
		<link>http://blog.earlh.com/index.php/2009/09/querying-databases-in-r-on-mac-os/</link>
		<comments>http://blog.earlh.com/index.php/2009/09/querying-databases-in-r-on-mac-os/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 07:06:38 +0000</pubDate>
		<dc:creator>earl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[greenplum]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[R and Databases]]></category>
		<category><![CDATA[R Tips]]></category>

		<guid isPermaLink="false">http://blog.earlh.com/?p=472</guid>
		<description><![CDATA[Unfortunately, it appears with the recent release of 10.6 / Snow Leopard Apple has removed the ODBC Administrator Tool from the OS. It can still be downloaded from Apple.]]></description>
			<content:encoded><![CDATA[<p>Unfortunately, it appears with the recent release of 10.6 / Snow Leopard Apple has removed the ODBC Administrator Tool from the OS.  It can still be downloaded <a href="http://www.apple.com/downloads/macosx/apple/macosx_updates/odbcadministratortoolformacosx.html"> from Apple</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.earlh.com/index.php/2009/09/querying-databases-in-r-on-mac-os/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Querying Databases in R</title>
		<link>http://blog.earlh.com/index.php/2009/08/querying-databases-in-r/</link>
		<comments>http://blog.earlh.com/index.php/2009/08/querying-databases-in-r/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 16:00:36 +0000</pubDate>
		<dc:creator>earl</dc:creator>
				<category><![CDATA[Data Munging]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[R Tip]]></category>
		<category><![CDATA[data frame]]></category>
		<category><![CDATA[greenplum]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[R and Databases]]></category>
		<category><![CDATA[R Tips]]></category>

		<guid isPermaLink="false">http://blog.earlh.com/?p=449</guid>
		<description><![CDATA[One of the first things you&#8217;ll want to do in R is set it up to talk to databases. The easiest way to do this is using ODBC, via package RODBC. To get the package, run > install.packages(RODBC) Once you &#8230; <a href="http://blog.earlh.com/index.php/2009/08/querying-databases-in-r/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One of the first things you&#8217;ll want to do in R is set it up to talk to databases.  The easiest way to do this is using ODBC, via package RODBC.</p>
<p>To get the package, run<br />
<code>
<pre class="brush:text;">
> install.packages(RODBC)
</pre>
<p></code></p>
<p>Once you have RODBC installed, you call it in R as follows.  But it&#8217;s very simple: a bit of setup, then sqlQuery will run your sql and return the results in a data frame.<br />
<code>
<pre class="brush: text;">
library(RODBC)

db <- odbcConnect( dsn='your dsn name' )
sql <- 'select page_id, count(*) as cnt
           from document_ads
           group by page_id
           having count(*) > 1'

results <- sqlQuery(db, sql, errors=T, rows_at_time=1024)
str(results)
'data.frame':	282432 obs. of  2 variables:
 $ page_id: int  17646774 17115332 17606022 15899428 17099174 17283774 8604200 16315025 17259751 17283270 ...
 $ cnt            : int  489 1119 132 113 148 200 112 121 1135 633 ...
</pre>
<p></code></p>
<p>On Windows, you setup the DSNs in the ODBC Data Sources inside the control panel; on MacOS, mysql includes a program called ODBC Administrator; on linux, you'll have to install <a href="http://www.easysoft.com/developer/interfaces/odbc/linux.html"> unixODBC </a>.</p>
<p>Also, it's often convenient to write code that caches your query results, particularly if the query takes a while.  I've found that the easiest thing to do is write the results into a data file and check for the file existence like such:<br />
<code>
<pre class="brush:text;">
filename <- 'query cache.RData'
if (!file.exists(filename)){
   # don't have a cached copy so run the query
   library(RODBC)
   [snip]
   query1 <- sqlQuery(db, sql, errors=T, rows_at_time=1024)

   # save the query results for the future
   save(list=c('query1', 'sql'), file=filename)
   rm(list=c('query1', 'sql') )
}
load(file=filename)
</pre>
<p></code</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.earlh.com/index.php/2009/08/querying-databases-in-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>R Dates &#8211; Recovering and Converting From Integers</title>
		<link>http://blog.earlh.com/index.php/2009/08/r-dates-recovering-and-converting-from-integers/</link>
		<comments>http://blog.earlh.com/index.php/2009/08/r-dates-recovering-and-converting-from-integers/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 16:00:01 +0000</pubDate>
		<dc:creator>earl</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[R Tip]]></category>
		<category><![CDATA[R Tips]]></category>

		<guid isPermaLink="false">http://blog.earlh.com/?p=408</guid>
		<description><![CDATA[One problem with R is that dates (class Date) are internally stored as integer numbers of days elapsed since 1 January 1970 and R sometimes loses the dateness of the variables and thinks of it only as an integer. So &#8230; <a href="http://blog.earlh.com/index.php/2009/08/r-dates-recovering-and-converting-from-integers/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One problem with R is that dates (class Date) are internally stored as integer numbers of days elapsed since 1 January 1970 and R sometimes loses the dateness of the variables and thinks of it only as an integer.  So in the first line, we take the range of dates present in our data, allowing for the fact that some may be skipped.  These are just integers &#8212; try looking at the output from min(m2$day):max(m2$day).  To turn these back into dates we add them to the first date object:<br />
<code>
<pre class="brush:text;">
> min(m2$day):max(m2$day)
 [1] 14245 14246 14247 14248 14249 14250 14251 14252 14253 14254 14255 14256 14257 14258 14259 14260 14261 14262 14263 14264 14265 14266 14267 14268 14269 14270 14271
[28] 14272 14273 14274 14275 14276 14277 14278 14279 14280 14281 14282 14283 14284 14285 14286 14287 14288 14289 14290 14291 14292 14293 14294 14295 14296 14297 14298
[55] 14299 14300 14301 14302 14303
>
> as.integer(as.Date('1970-01-01'))
[1] 0
>
> # the fix:
> as.Date('1970-01-01') + min(m2$day):max(m2$day)
 [1] "2009-01-01" "2009-01-02" "2009-01-03" "2009-01-04" "2009-01-05" "2009-01-06" "2009-01-07" "2009-01-08" "2009-01-09" "2009-01-10" "2009-01-11" "2009-01-12"
[13] "2009-01-13" "2009-01-14" "2009-01-15" "2009-01-16" "2009-01-17" "2009-01-18" "2009-01-19" "2009-01-20" "2009-01-21" "2009-01-22" "2009-01-23" "2009-01-24"
[25] "2009-01-25" "2009-01-26" "2009-01-27" "2009-01-28" "2009-01-29" "2009-01-30" "2009-01-31" "2009-02-01" "2009-02-02" "2009-02-03" "2009-02-04" "2009-02-05"
[37] "2009-02-06" "2009-02-07" "2009-02-08" "2009-02-09" "2009-02-10" "2009-02-11" "2009-02-12" "2009-02-13" "2009-02-14" "2009-02-15" "2009-02-16" "2009-02-17"
[49] "2009-02-18" "2009-02-19" "2009-02-20" "2009-02-21" "2009-02-22" "2009-02-23" "2009-02-24" "2009-02-25" "2009-02-26" "2009-02-27" "2009-02-28"
>
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.earlh.com/index.php/2009/08/r-dates-recovering-and-converting-from-integers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Removing Extra Column of Data from CSVs in R &#8212; R Tip</title>
		<link>http://blog.earlh.com/index.php/2009/07/removing-extra-column-of-data-from-csvs-in-r-r-tip/</link>
		<comments>http://blog.earlh.com/index.php/2009/07/removing-extra-column-of-data-from-csvs-in-r-r-tip/#comments</comments>
		<pubDate>Sat, 11 Jul 2009 16:00:19 +0000</pubDate>
		<dc:creator>earl</dc:creator>
				<category><![CDATA[Data Munging]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[R Tip]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[R Tips]]></category>

		<guid isPermaLink="false">http://blog.earlh.com/?p=57</guid>
		<description><![CDATA[When R writes a csv file, you get an extra column of data as such: > s > plot(x=s$x, y=s$y ) > > write.csv(x=s, file='s0.csv' ) When you peek in the csv file, you see this: blog earl$ head s0.csv &#8230; <a href="http://blog.earlh.com/index.php/2009/07/removing-extra-column-of-data-from-csvs-in-r-r-tip/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When R writes a csv file, you get an extra column of data as such:<br />
<code>
<pre class="brush:text;">
> s <- data.frame(x=1:30, y=10*runif(n=30) )
>
> plot(x=s$x, y=s$y )
>
> write.csv(x=s, file='s0.csv' )
</pre>
<p></code></p>
<p>When you peek in the <a href='http://blog.earlh.com/wp-content/uploads/2009/07/s0.csv'>csv</a> file, you see this:<br />
<code>
<pre class="brush: text;">
blog earl$ head s0.csv
"","x","y"
"1",1,8.29164186026901
"2",2,2.83956938423216
"3",3,7.43510165950283
"4",4,6.38210728997365
"5",5,9.29241271456704
"6",6,6.13102467032149
"7",7,5.03747826907784
"8",8,1.83257902506739
"9",9,9.62789378128946
blog earl$
</pre>
<p></code></p>
<p>What is that first column?  It&#8217;s actually pretty obvious in this example, but if you&#8217;ve sorted your data frame a couple times it will appear to be a random sequence of integers.  It&#8217;s the row names from your data frame, and to suppress it, you use row.names = false:<br />
<code>
<pre class="brush:text;">
> write.csv(x=s, file='s1.csv', row.names=F)
>
</pre>
<p></code></p>
<p>checking, we see the <a href='http://blog.earlh.com/wp-content/uploads/2009/07/s1.csv'>csf file</a> looks like:<br />
<code>
<pre class="brush: text;">
earl$ head s1.csv
"x","y"
1,8.29164186026901
2,2.83956938423216
3,7.43510165950283
4,6.38210728997365
5,9.29241271456704
6,6.13102467032149
7,5.03747826907784
8,1.83257902506739
9,9.62789378128946
blog earl$
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.earlh.com/index.php/2009/07/removing-extra-column-of-data-from-csvs-in-r-r-tip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

