R Project resource

Excellent website full of useful information on using R Project:

http://gastonsanchez.com/

Topics include:
Getting data from the web
Data Manipulation
Data visualization

This is probably my favorite project on his site – not because it’s Star Wars, but because of the way the data is presented:

http://gastonsanchez.com/work/starwars/

Unpivoting a Table

I have a pivot table with Routes going down and years going across, but I need to normalize it – have a column for the route and a column for the year and then a value for each route, column.

Route2015201620172018
North12141824
South23211917
East19241210
West45585759
Desert231031
Plains23232323
Scenic49483738
Bad Lands121288
Roundabout3336

Oracle and MS SQL databases have a function called UNPIVOT.

MySQL does not have a similar function yet, so you have to do a SELECT, UNION ALL type command.

In R Project, the function is called “Melt”

Doing this in R Project:

1. Install the reshape library if you don’t have it.
2. Program looks like so:


library(reshape)
rte_vol=read.csv("/users/sarod/documents/route_volume.csv")
rte_vol_melted<-melt(rte_vol, id=c("Route")) rte_vol_melted

Line 1 runs the reshape library
Line 2 reads the data in
Line 3 "Melts" the data
Line 4 Shows the output

Intro to R-project

A second start at learning r-project.

Some very useful commands: Importing a csv file

To view the contents of the file that was just imported

Note that this isn’t a command – this is the name used from the command to import the file.

What is the current working directory

When the file was imported, you could pull up the data using the variable name, which would be yrseq in this case. The following command makes the same thing true for all the column names.

After putting in the above command, I can now type in the name of a field and get the data list. “tpd_base” for example.

List the names of the datasets loaded

List the names of variable in a given dataset

Starting Rserve Using Tableau with R-Project

References for the above:

R Tutorial Dr. Mark Gardner

mapproj in r

Making maps with r using mapproj.

 

library(ggmap)

map <- get_map(location=’North America’, zoom=6)

ggmap(map)

 

What each line does:

Line 1 loads the ggmap library.

Line 2 fetches the terrain map graphic from Google’s server at the specified zoom level – higher is closer in.  Zoom level 6 is about the size of Texas or California.  Zoom 15 is about the level of a neighborhood and is the max.  Going higher does zoom in further but there is no background.  The location value seems to be able to take any value that google maps would take.  The image is stored as “map”.

Line 3 displays the map.

Another example

library(ggmap)

a <- get_map(location=’ft worth, tx’,zoom=12)
ggmap(a)

Maps in R Project

Found some great information on how to make maps in R Project:

Maps in R: Introduction – Drawing the map of Europe

To install libraries, use the command “install.packages()”

This will bring up a list of packages to install, then you can select the package you need.  I was a little disappointed that I couldn’t put the package in the command line, like this: “install.packages(rworldmap)” and then it would download directly.  Maybe it actually can, but I wasn’t using the command correctly.