A Few R Data Types

Every computer programming environment (i.e. R, SAS, SPSS, Python, Perl, etc.) offers various data types. Data types are variables that are used during the run time (when a computer program is running) to hold a value or obtain the current value from the variable. For example let's say we are wanting to run an analysis for a farm which grows apples, peaches, strawberries and raspberries. In the analysis, we may want to hold an integer value in a computer program where this integer value signifies the number of fruits we packed in the last box (i.e. how many apples, etc.); similarly we may want to hold a string value in the same computer program that signifies the name of the fruit we packed in the last box (i.e. apples), etc. Accordingly an integer and a string are two of the common data types offered in many programming environments such as Python and Perl. Every programming environment offers multitude data types, in this topic we will review the three most common data types offered by R: vectors, list and tables.

Quiz

Question

A "Vector" variable in R can hold a series of values of a given type (say Integer).

Looks Good! Correct: Explanation: A Vector in R is a container to hold a series of values of a data type
Question

If we have an R "Vector" defined by the statement:

aQuestionVector <- c(13,12,5,8,15)

What does the notation aQuestionVector[3] refer to?

Looks Good! Correct: Explanation: The index in a Vector in R starts at 1 when counted from the left most cell
Question

We know that a R "List" may be visualized as a Vector of Vectors - Can the individual Vectors inside a list have different sizes (i.e. number of cells)?

Looks Good! Correct: Explanation: The individual Vectors can have any dimension

We have found it to be much easier to learn these concepts if you have the opportunity to practice with some data. Let's start with some simple exercises to help you practice.  

Exercise 1

Write a script in which you have defined a vector with 5 cells having values 2,4,8,16, and 32. Next write an instruction with a new variable that will calculate the value of the sum of all the integer values of the defined vector. Finally print the sum from the variable.

Answer: You must get an output of 62 on the R console i.e. the lower left frame of RStudio.

Exercise 2

Modify the input data file for the Vector Table (from the data file link above the video tutorial) and change the column names to GroupingVariable, Att1, Att2 instead of GroupOn,Attribute1,Attribute2 respectively. Now print the vector form for the column 1 of the table.