Chapter 4 Vectors | Introduction To Programming With R - DiSCDown
Maybe your like
4.1 Objects in R
R is an object-oriented programming language with the fundamental design principle: Everything in R is an object. In R, objects can be:
- Variables (e.g., a, b, result).
- Functions (e.g., mean(), max(), sin()).
- Connection handlers.
- …
Remember the very simple example from the chapter First Steps where we calculated \(a^2\) as follows?
a <- 5 a^2We assign 5 to a. This automatically creates a new object (a) and stores the desired value in it (here 5).
Object orientation: All objects in R have a specific class that can have a set of class-specific attributes as well as methods for certain generic functions. In this simple example, a gets a numeric vector of length 1 (i.e., containing one numeric value).
Know the basics!
Vectors are sometimes also referred to as the ‘nuts & bolts’ in R as they build the basis for all complex objects such as data frames or fitted regression models. The image below shows a (simplified) schematic overview of how various more complex objects (on the right hand side) are based on vectors (on the left hand side).
Figure 4.1: Simplified schematic overview of how different R objects, covered in this book, are connected.
Frequently-used data types can also be distinguished by their dimensionality (1-d, 2-d, n-d) and whether they are homogenous (with all elements of the same type) vs. heterogenous (with elements of potentially different types).
| Dimension | Homogenous | Heterogenous |
|---|---|---|
| 1 | Atomic vectors | Lists |
| 2 | Matrix | Data frame |
| \(\ge 1\) | Array |
In this course, we will learn about vectors (atomic vectors), matrices (2-d arrays), factors, date and date time objects, lists, and data frames. In other situations, you will most certainly come across many other more complex objects, but keep in mind that most of them are created by using basic vectors (i.e., lists and atomic vectors).
Thus: Knowing these basics is essential to becoming a good programmer!
Print objects
When working on the interactive R console we can always print objects to see what is stored in them. This can be done by simply entering the name of the object and pressing enter:
a ## [1] 5What happens is ‘implicit printing’ (we have not explicitly asked for it). Explicit printing would be if we call the print() function:
print(a) ## [1] 5Both have the same effect when working on the interactive console. When writing scripts, the print() function becomes more important as implicit printing will no longer work (or only in special situations).
One way to (i) create new objects and (ii) directly print them is by adding another pair of round brackets around the command. Instead of doing it in two steps
a <- 5^2 a ## [1] 25we can call (a <- 5^2). The additional brackets cause implicit printing because the result of the assignment to a is evaluated, thus creating the same effect as the two separate lines above.
(a <- 5^2) ## [1] 25This is sometimes useful for brevity, avoiding additional typing and saving some space in the displayed code.
Tag » What Is A Vector In R
-
R Vector - Javatpoint
-
Vectors And Functions In R | DataScience+
-
Vector | R Tutorial
-
R Vector: Create, Modify And Access Vector Elements - DataMentor
-
R - Vectors - Tutorialspoint
-
R Vectors - W3Schools
-
How To Create, Combine And Index Vectors In R? - TechVidvan
-
2.1 What Is A Vector? | Beginning Computer Science With R
-
Basic Vectors In R Programming: Definition & Purpose
-
R - Vector - GeeksforGeeks
-
Introduction To Vectors In R - Towards Data Science
-
What Is A Vector In R? - Quora
-
R Tutorial - How To Create And Name Vectors In R - YouTube
-
20 Vectors | R For Data Science