Pages

Friday, January 24, 2014

Add leading zeros in R

Morning coffee report...I have recently had two occasions where I needed to add leading zeros to data set.  One was working with latitude/longitude data and the second occurred when trying to plot data aggregated by zipcode.  The problem was that zipcodes that begin with zero did not contain that number and was not completely straight-forward as the number of leading zeros varied (sometime one, sometimes more than one).  After seemingly too much searching, I discovered the formatC function which accomplished exactly what I was looking for.  Below is a sample example with code that will hopefully serve as a refresher next time I need to do this.




 
Example input data without leading zeros (no zeros) and with leading zeros (leadzeros) added using the formatC function.
nozeros leadzeros
1 00001
12 00012
123 00123
1234 01234
12345 12345






Thursday, January 16, 2014

Importing data from Dropbox to R


Dropbox (www.dropbox.com) is one of several cloud-based data file sharing applications.  I find it useful to store and share data that is backed up automatically and accessible from anywhere.  Additionally, files can be shared and used by others.  Data from this site can be imported directly into R and eliminates local file storage (usually, see next post for exceptions), and eases development of reproducible research.  Below an example of importing a comma delimited text file (csv).  

To get started you must get a link from your Dropbox file using the share link tool that provides a browser-friendly link that you can share. Unfortunately, this link alone will not work as this assumes a user will click on the link to open the file and requires one additional step. 

Using the "dat" dataset as an example hosted on the shared folder on my dropbox.  Using the share link tool provides me with the following url: https://www.dropbox.com/s/tdtwlwbsssl0wpc/dat.csv.


To import into R, the link must be edited to the following: http://dl.dropbox.com/s/tdtwlwbsssl0wpc/dat.csv.  Notice only the first section is changed. 




Wednesday, January 8, 2014

New home of www.Statisticsanddatamining.com

Greeting, this will be the new home of R, statistics, and datamining related posts and discussions.  More to come soon.  If you have looked at www.statisticsanddatamining.com before this will eventually replace it.