Data Analysis with R (Part 1): Introduction to R Programming

Source: https://campus.datacamp.com/

Selection by comparison

Select values in that are larger than 10. Assign the result to the variable

Print the selected elements

Matrices

In R, a matrix is a collection of elements of the same data type (numeric, character, or logical) arranged into a fixed number of rows and columns.

You can construct a matrix in R with the matrix() function. Consider the following example:

In the matrix() function:

  • The first argument is the collection of elements that R will arrange into the rows and columns of the matrix. Here, we use 1:9 which constructs the vector c(1, 2, 3, 4, 5, 6, 7, 8, 9).
  • The argument byrow indicates that the matrix is filled by the rows. This means that the matrix is filled from left to right and when the first row is completed, the filling continues on the second row. If we want the matrix to be filled by the columns, we just place byrow = FALSE.
  • The third argument nrow indicates that the matrix should have three rows.
  • The fourth argument ncol indicates the number of columns that the matrix should have

Another example

Factors

The term factor refers to a statistical data type used to store categorical variables.

Dataframes

A dataframe is a collection of elements of the different data types (numeric, character, or logical) arranged into a fixed number of rows and columns.

The elements in the matrix should be of the same type. But a dataframe doesn’t have this restriction.

How to inspect a dataframe?

There are several functions you can use to inspect your dataframe. To name a few

  • : this by default prints the first 6 rows of the dataframe
  • : this by default prints the last 6 rows to the console
  • : this prints the structure of your dataframe
  • : this by default prints the dimensions, that is, the number of rows and columns of your dataframe
  • : this prints the names of the columns of your dataframe

For example,

How to construct a dataframe?

Suppose you want to construct a data frame that describes the main characteristics of eight planets in our solar system. The main features of a planet are:

  • The type of planet (Terrestrial or Gas Giant).
  • The planet’s diameter relative to the diameter of the Earth.
  • The planet’s rotation across the sun relative to that of the Earth.
  • If the planet has rings or not (TRUE or FALSE).

You construct a data frame with the function. As arguments, you should provide the above mentioned vectors as input that should become the different columns of that data frame. Therefore, it is important that each vector used to construct a data frame has an equal length. But do not forget that it is possible (and likely) that they contain different types of data.

Indexing and selecting columns from a dataframe

In the same way as you indexed your vectors, you can select elements from your dataframe using square brackets. Different from dataframes however, you now have multiple dimensions: rows and columns. That’s why you can use a comma in the middle of the brackets to differentiate between rows and columns. For instance, the following code would select the element in the first row and the second column from the dataframe .

You can also use the operator to select an entire column from a dataframe. For instance, would select the entire planets column from the dataframe planet_df.

Example

Lists

A list in R is similar to your to-do list at work or school: the different items on that list most likely differ in length, characteristic, type of activity that has to do be done.

A list in R allows you to gather a variety of objects under one name (that is, the name of the list) in an ordered way. These objects can be matrices, vectors, data frames, even other lists, etc. It is not even required that these objects are related to each other.

You can easily construct a list using the function. In this function you can wrap the different elements like so: .

Selecting elements from a list

Your list will often be built out of numerous elements and components. Therefore, getting a single element, multiple elements, or a component out of it is not always straightforward. One way to select a component is using the numbered position of that component. For example, to “grab” the first component of you type

Another way to check is to refer to the names of the components: selects the vector.

A last way to grab an element from a list is using the sign. The following code would select from : .

Besides selecting components, you often need to select specific elements out of these components. For example, with you select from the first component of the first element. This would select the number 1.

Getting help

How to Create Functions

Getting your data into R

One important thing before you actually do analyses on your data, is that you will need to get your data into R. R contains many functions to read in data from different formats. To name only a few:

  • : Reads in tabular data such as txt files
  • : Read in data from a comma-separated file format
  • : Reads in an excel worksheet
  • : Reads in data from .sav SPSS format.

For the current exercise, we have put the R mtcars dataset into a csv file format and put this on github. The data can be found on the following link:

Sample data

http://s3.amazonaws.com/assets.datacamp.com/course/uva/mtcars.csv

Define the separator

Working directories in R

Default working directory where R will look for

C:/Users/Username/documents.

Of course this working directory is not static and can be changed by the user.

In R there are two important functions:

  • : This function will retrieve the current working directory for the user
  • : This functions allows the user to set her own working directory
  • lists all the files that exists in your working directory.

Example code

Importing R packages

Imagine we want to do some great plotting and we want to use ggplot2 for it. If we want to do so, we need to take 2 steps:

  1. Install the package ggplot2 using
  2. Load the package ggplot2 using or

Example code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store