Alex Rivera | Logout

Set up large database in MySQL for analysis in R

Asked 2010-07-27T03:33:12.453
10

I have reached the limit of RAM in analyzing large datasets in R. I think my next step is to import these data into a MySQL database and use the RMySQL package. Largely because I don't know database lingo, I haven't been able to figure out how to get beyond installing MySQL with hours of Googling and RSeeking (I am running MySQL and MySQL Workbench on Mac OSX 10.6, but can also run Ubuntu 10.04).

Is there a good reference on how to get started with this usage? At this point I don't want to do any sort of relational databasing. I just want to import .csv files into a local MySQL database and do the subsetting in with RMySQL.

I appreciate any pointers (including "You're way off base!" as I'm new to R and newer to large datasets... this one's around 80 mb)

Edit
Report

2 Answers

5

I have a pretty good suggestion. For 80MB use SQLite. SQLite is a super public domain, lightweight, super fast file-based database that works (almost) just like a SQL database. http://www.sqlite.org/index.html

You don't have to worry about running any kind of server or permissions, your database handle is just a file.

Also, it stores all data as a string, so you don't even have to worry about storing the data as types (since all you need to do is emulate a single text table anyway).

Someone else mentioned sqldf: http://code.google.com/p/sqldf/

which does interact with SQLite: http://code.google.com/p/sqldf/#9._How_do_I_examine_the_layout_that_SQLite_uses_for_a_table?_whi

So your SQL create statement would be like this

create table tablename (
  id INT(11) INTEGER PRIMARY KEY,
  first_column_name TEXT,
  second_column_name TEXT,
  third_column_name TEXT 
);

Otherwise, neilfws' explanation is a pretty good one.

P.S. I'm also a little surprised that your script is choking on 80mb. It's not possible in R to just seek through the file in chunks without opening it all up in memory?

answered 2010-07-27T06:57:37.680
2

The sqldf package might give you an easier way to do what you need: http://code.google.com/p/sqldf/. Especially if you are the only person using the database.

Edit: Here is why I think it would be useful in this case (from the website):

With sqldf the user is freed from having to do the following, all of which are automatically done:

  • database setup
  • writing the create table statement which defines each table
  • importing and exporting to and from the database
  • coercing of the returned columns to the appropriate class in common cases

See also here: Quickly reading very large tables as dataframes in R

answered 2010-07-27T06:48:12.427

Your Answer