Learning Try R



R is a tool for statistics and data modeling. It is an elegant and versatile programming language and has a highly expressive syntax designed around working with data. R also includes extremely powerful graphics capabilities. If you want to easily manipulate your data and present it in compelling ways, R is the tool for you.

The seven functions of R :

  1. Try R
  2. Vectors
  3. Matrices
  4. Summary Statistics
  5. Factors
  6. Data Frame
  7. Real-World Data

Chapter One (Try R) tells you the basic R expressions with numbers, strings  and True/False values. How to store those values in variables and pass them to functions and how to get help  on functions. For instance, with “Expression” you can type anything at the prompt and R will evaluate it and print the answer, e g  > 1 + 1 gives you [1]  2, likewise if you type the string  “Arr,  matey!” It gives you [1] “Arr, matey!”

The “Logical value” like TRUE or FALSE which can be refered to as “Boolean” values, e g > 3 > 4 gives [1] TRUE and another a double-equals sign to check whether two values are equal, e g > 2 + 2 == 5 [1] FALSE

Variables” with this  you can store values into a variable to access it later, e g  > x <- 42 to store a value in x

Functions” you can call a function by typing its name, followed by one or more arguments to that function in parenthesis, e g > sum(1,  3,  5) gives [1]  9

Help” help (functionname) brings up help for the given function.

Files“,   “R”  commands can also be written in plain text file with a R extension by convention for executing later by calling “list.files” function

Chapter Two (Vectors)  is  list of values that R rely on for many of its operations. Like Sequence which use the start:end notation e g seq(5,  9) which gives [1] 5 6 7 8 9.

Vector Access which retrieves values within a vector by providing its numeric index in square brackets  by using “sentence” variable e g “sentence [3]” after this <- c(‘walk’, ‘the’, ‘plank’) gives [1] “plank”.

Vector Names, you can assign names to vector’s elements  by passing a second vector filled with names to the ‘names’. assignment function, e g  names(ranks) <- c(“first”, “second”, “third”) after > ranks <- 1:3

Plotting One Vector, barplot function draws a bar chart with vector’s values  by typing barplot(vesselsSunk) after > vesselsSunk <-  c(4, 5, 1)

Vector Math” with this , you can add a single value to a vector and the scalar will be added to each values in the vector, returning a new vector with the results, e g typing a + 1 after <- c(1, 2, 3) gives [1] 2 3 4. The same is of division, multiplication, or any other basic arithmetic.

“Scatter Plots” this takes two vector, one for X value and one for Y value and draws a graph of them.

“NA Value” this can be useful when working with sample data, and a given value isn’t available, but it’s not a good idea to just throw those values out. R has a value that explicitly indicates a sample was not available.

Chapter Three (MATRICES)  this is just a fancy term for a 2-dimensional array, it helps when needs data in rows and column. For instance, making a matrix 3 rows high by 4 column wide with all its fields set to 0: type “matrix(0, 3, 4) into the console and you get the result below

there are still other functions of matrices like MATRIX ACCESS and MATRIX PLOTTING.

Chapter Four (SUMMARY STATISTICS) this explain data. It has Mean, Median, and Standard Deviation functions and how they can be displayed on graph.

Chapter Five (FACTORS) this help to group data by  categories. R uses Factor function to track categorized values. For instance

Chapter Six (DATA FRAMES) R uses this to structure data just like in Excel spreadsheet or database table. In this chapter you can use Data frame Access to access individual potions of a data frame, you can load data from external files with Loading Data Frame and with Merging Data Frame, you can join two data frames together, using the content of one or more columns.

Chapter Seven (REAL-WORLD DATA) is about correlating charts, adding some of R  extra functionality such as ggplot2 which gives a better appearance of the charts

Bar and Line Graph ggplot2



To achieve the above ggplot2 graph

I googled http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2)/    Cookbook for R

I opened my R console, opened a new script called R editor by clicking on file on the top-left corner of the R console, then, I went back to the R cookbook

Firstly, I copied some sample data (derived from the tips datasets in the reshape2 package) under the “Bar Graph of  Value”
dat time total_bill
#> 1 Lunch 14.89
#> 2 Dinner 17.23

# Load the ggplot2 package
and pasted into my R editor and then Wright clicked, ran it, which automatically appears in my R console


Secondly, I went back to the R cookbook, copied and pasted some variable mappings which consist: (time:x-axis and sometimes color fill and (total_bill : y-axis into my R editor, wrigth clicked and ran it which automatically popped up the graph above in my R graphic device









In order to achieve all the graphs above, I copied and pasted all data below into my R console and run them one after the other.
# Very basic bar graph
ggplot(data=dat, aes(x=time, y=total_bill)) +

# Map the time of day to different fill colors
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +

## This would have the same result as above
# ggplot(data=dat, aes(x=time, y=total_bill)) +
# geom_bar(aes(fill=time), stat=”identity”)

# Add a black outline
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(colour=”black”, stat=”identity”)

# No legend, since the information is redundant
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(colour=”black”, stat=”identity”) +

In conclusion, it is a great tool to work with if you can use is properly.









Published by


Digital Marketing Strategist

Leave a Reply

Your email address will not be published. Required fields are marked *