From the course: R for Data Science: Lunch Break Lessons
Vector math
From the course: R for Data Science: Lunch Break Lessons
Vector math
- One of the immediate things you'll bump into with the R programming language is just that it deals with math a little differently than how another standardized programming language might approach it. Let's take a look at that. So for example, if I have a variable or in this case, a vector, how would I multiply each item in that vector by two? First of all, let's create some data we can use here, so I'm going to create something called many.numbers, and into that variable, I'm going to assign the numbers one through nine, so I'll go to one... And hit return, and you'll see that in the global environment to the right I now have a vector called many.numbers that contains the numbers one through nine. Incidentally there's a much quicker way to do this, I can type many.numbers and assign into it 1:9, and that produces exactly the same thing as what I produced before. So now that I have many numbers I would like to multiply each item by two and in the standard language, you would set this up with a for loop. So for something like this it would look for, and then parenthesis a number in many.numbers and then you'll put in curly braces and a space, and we'll just print it out. Print, parenthesis, a number times two. And if I hit return, now that goes through each item in the vector and multiplies it by two. Now, r does this differently, let me show you how r would do it. In this case I would type in many.numbers followed by multiplied by two. And you can see what r has done, has taken each element of many.numbers and multiplied that by two. I can save this result for later by assigning it to a variable, so I'll call something two.times, and put into two.times, many.numbers times two. And you can see over here in the global environment that I have a vector now called two.times with nine numbers and it's been multiplied by two for each of those. So this works for all math operations. So for example I can do many.numbers divided by two and you'll see that the result is each number in many.numbers divided by two. R allows interactions between two vectors. So let's create another vector, we'll call it more.numbers and in it we'll assign... Oh let's assign some random numbers. And you can see that we now have a vector called more.numbers with nine elements and there's a random set of numbers in there. So I can multiply or add those, divide them or subtract them. Let's go ahead and- many.numbers plus more.numbers, and the result will be the first element of many.numbers, added to the first element of more.numbers. So the first element of many.numbers, if I look in the upper right-hand corner in environment, the first element of many.numbers is one, and that's added to the first element of more.numbers which happens to be two, and so the result is three. You can see the result down here is three. So many.numbers plus more.numbers adds each element by element, and of course you can do that with all math operations. One more thing, what happens if the vectors are different lengths? So let's create another vector, I'm going to type down here, short.vector and assign into that, let's just assign three numbers. And now I have a vector called short.vector. What happens if I add many.numbers plus a short.vector? Well, it does something called recycling and what you'll observe here is just that the first number of many.numbers happens to be one. The first number of short.vector happens to be two, and the result is 1 + 2 = 3. Same thing for the second number, 2 + 3 = 5, and 3 + 4 = 7. Now we've just run out of short.vector numbers, so R recycles short.vector, and what we get is four plus the first number of short.vector is two, which is equal to six and so on. So 5 + 3 = 8, and 6 + 4 = 10, that's called recycling. This works because short.vector happens to be a multiple in length of many vectors. If that's not true... Let's create something that will break that, let's call it short.odd.vector, and into it we'll assign four numbers. Now if I attempt to add those two you'll see that we get a warning message, that the longer object length is not a multiple of the shorter object length. So when you're dealing with vectors the idea is that math will work differently as far as adding those two vectors together. You don't need to use a for loop, all of this is built into the way that R thinks.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
-
Exercise File: Subscribe to access.
Ex_Files_R_Data_Lunch_Break.zip -
Exercise File: Subscribe to access.
Ex_Files_R_for_Data_Sci_2021_Q3.zip -
Exercise File: Subscribe to access.
Ex_Files_R_for_Data_Sci_2021_Q4.zip -
Exercise File: Subscribe to access.
Ex_Files_R_for_Data_Sci_2022_Q1.zip -
Exercise File: Subscribe to access.
Ex_Files_R_for_Data_Sci_2022_Q2.zip -
Exercise File: Subscribe to access.
Ex_Files_R_for_Data_Sci_2022_Q3.zip -
Exercise File: Subscribe to access.
Ex_Files_R_for_Data_Sci_2022_Q4.zip -
Exercise File: Subscribe to access.
Ex_Files_R_for_Data_Sci_2023_Q1.zip -
Exercise File: Subscribe to access.
Ex_Files_R_for_Data_Sci_2023_Q2.zip
Download courses and learn on the go
Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.
Contents
-
-
R built-in data sets5m 21s
-
Vector math5m 57s
-
(Locked)
Subsetting7m 17s
-
(Locked)
R data types: Basic types7m 34s
-
(Locked)
R data types: Vector5m 16s
-
(Locked)
R data types: List5m 27s
-
(Locked)
R data types: Factor5m 15s
-
(Locked)
R data types: Matrix8m 48s
-
(Locked)
R data types: Array3m 50s
-
(Locked)
R data types: Data frame6m 44s
-
(Locked)
Data frames: Order and merge8m 10s
-
(Locked)
Data frames: Read and update4m 44s
-
-
-
Data frames: rbind3m 4s
-
Dataframes: cbind3m 11s
-
(Locked)
apply and lapply3m 21s
-
(Locked)
mapply2m 21s
-
(Locked)
plot2m 45s
-
(Locked)
Brackets and double-brackets2m 50s
-
(Locked)
mean, rowMeans, and colMeans1m 49s
-
(Locked)
RSQLite3m 44s
-
(Locked)
sqldf2m 9s
-
(Locked)
Aggregate3m 17s
-
(Locked)
Random numbers4m 26s
-
(Locked)
Pipeline4m 42s
-
(Locked)
Working with clipboards2m 45s
-
-
-
barplot2m 31s
-
Pie chart2m 11s
-
(Locked)
unlist3m 24s
-
(Locked)
Joins: Inner and full3m 3s
-
(Locked)
Joins: Left and right2m 13s
-
(Locked)
Sets: Union, intersect, and difference2m 10s
-
(Locked)
Sets: Equal and in2m 14s
-
(Locked)
colors2m 25s
-
(Locked)
ifelse3m 5s
-
(Locked)
spineplot2m 36s
-
(Locked)
browser3m 37s
-
(Locked)
debugonce2m 25s
-
(Locked)
Default mirror2m 31s
-
-
-
Dealing with NA6m 1s
-
Using with()2m 55s
-
(Locked)
Simple string matching4m 35s
-
(Locked)
grep2m 53s
-
(Locked)
dotchart3m 54s
-
(Locked)
fourfoldplot3m 34s
-
(Locked)
matplot3m 50s
-
(Locked)
dimnames5m 4s
-
(Locked)
mosaicplot4m 23s
-
(Locked)
stemplot2m 57s
-
(Locked)
stripchart3m 10s
-
(Locked)
sunflower2m 57s
-
(Locked)
Switch2m 16s
-
-
-
Switch on factors2m 18s
-
Any/all4m 13s
-
(Locked)
sub, gsub, regex, and backreferences4m 52s
-
(Locked)
agrep and fuzzy matching4m 44s
-
(Locked)
combn finds combinations2m 33s
-
(Locked)
edit, fix, and dataentry4m 57s
-
(Locked)
zeallot5m 30s
-
(Locked)
menu2m 58s
-
(Locked)
person3m 16s
-
(Locked)
txtProgressBar3m 13s
-
(Locked)
zip and tar3m 50s
-
(Locked)
bitwise4m 11s
-
(Locked)
by is like tapply4m 15s
-
(Locked)
Update your R4m 1s
-
-
-
Be careful with transpose4m 45s
-
Passwords4m 45s
-
(Locked)
heatmap4m 24s
-
(Locked)
combine4m 11s
-
(Locked)
stopifnot2m 44s
-
(Locked)
weighted.mean2m 16s
-
(Locked)
chartr3m 50s
-
(Locked)
file.choose4m 2s
-
(Locked)
duplicated and unique2m 52s
-
(Locked)
load and save4m 23s
-
(Locked)
floor, round, ceiling, and trunc2m 32s
-
(Locked)
expand.grid2m 55s
-
(Locked)
Professional groups2m 26s
-
-
-
Simplify with c3m 29s
-
Logical operators5m 56s
-
(Locked)
char.expand3m 57s
-
(Locked)
complete.cases3m 16s
-
(Locked)
swirl2m 8s
-
(Locked)
tryCatch3m 23s
-
(Locked)
Double colons3m 5s
-
(Locked)
for loop4m 54s
-
(Locked)
The 100th episode4m 12s
-
(Locked)
while loop4m 18s
-
(Locked)
repeat loop4m 14s
-
(Locked)
Create your own swirl lesson4m 4s
-
(Locked)
Logic and flow control4m 2s
-
-
-
matrix, row, and column4m 41s
-
cumsum, cumprod, cummax, an dcummin4m 11s
-
(Locked)
issymetric3m 14s
-
(Locked)
file.access4m
-
(Locked)
file.info4m 1s
-
(Locked)
dput and dget4m 35s
-
(Locked)
Sort a data frame by multiple columns4m 12s
-
(Locked)
diag2m 52s
-
(Locked)
crossprod3m 13s
-
(Locked)
upper.tri and lower.tri3m 7s
-
(Locked)
strsplit() splits strings at matched characters2m 37s
-
(Locked)
Use setnames() to change the name of an object5m 3s
-
(Locked)
Change the structure of a vector with stack()4m 44s
-
-
-
Use droplevels() to simplify factors3m 26s
-
Use .Rmd for documentation7m 2s
-
(Locked)
Use rep() to create long repetitive vectors4m 58s
-
(Locked)
Use format() to improve readability4m 53s
-
(Locked)
Use pmax() and pmin() to discover the scope of paired vectors5m 18s
-
(Locked)
Use print() for more than you do now4m 55s
-
(Locked)
Use range() and extendrange() to analyze and manipulate groups of numbers3m 42s
-
(Locked)
Evaluate the importance of a number with rank()4m 51s
-
(Locked)
Use saveRDS() and readRDS() to serialize objects3m 26s
-
(Locked)
Use regular expressions with regexpr() and gregexpr()4m 22s
-
(Locked)
message5m 21s
-
(Locked)
regexpr5m 45s
-
(Locked)
diff4m 50s
-
-
-
exists1m 57s
-
formulas4m 42s
-
(Locked)
RPres5m 26s
-
(Locked)
lattice: Introduction5m 8s
-
(Locked)
lattice: xyplot5m 37s
-
(Locked)
lattice: cloud and wireframe4m 31s
-
(Locked)
lattice: contourplot4m 8s
-
(Locked)
lattice: barchart4m 57s
-
(Locked)
lattice: splom charts6m 14s
-
(Locked)
lattice: panels4m 50s
-
(Locked)
lattice: stripplot3m 18s
-
(Locked)
whichmin and whichmax2m 52s
-
(Locked)
par: font, size, color5m 10s
-
-
-
par: margins6m 21s
-
par: pch and points3m 17s
-
(Locked)
legend5m 26s
-
(Locked)
identical3m 28s
-
(Locked)
Matrix math: Overview of functions1m 38s
-
(Locked)
Matrix math review4m 50s
-
(Locked)
matrix: solve systems4m 11s
-
(Locked)
matrix: solve inverse3m 32s
-
(Locked)
matrix: backsolve and forwardsolve5m 24s
-
(Locked)
Matrix: Determinant3m
-
(Locked)
Arrays and outer2m 49s
-
(Locked)
Matrix: Crossproduct2m 7s
-
(Locked)
Matrix SVD and QR decomposition3m 39s
-
-
-
Matrix: Eigenvalues and eigenvectors1m 38s
-
Locator4m 38s
-
(Locked)
on.exit4m 11s
-
(Locked)
missing3m 11s
-
(Locked)
nargs2m 28s
-
(Locked)
tidyverse5m 43s
-
(Locked)
gutenbergr5m 4s
-
(Locked)
Create and clean a natural language corpus7m 25s
-
(Locked)
Remove stopwords from an NLP corpus5m 16s
-
(Locked)
NLP and term-document matrix5m 53s
-
-
-
Analyze term-document matrix5m 38s
-
NLP packages: Tidytext5m 7s
-
(Locked)
NLP packages: Quanteda7m 40s
-
(Locked)
NLP packages: Sentiment analysis8m 28s
-
(Locked)
Word clouds3m 10s
-
(Locked)
Hidden features of installr4m 1s
-
(Locked)
Use the Matrix package5m 29s
-
(Locked)
Create a sparse matrix4m 21s
-
(Locked)
Sparse matrices, triangles, and more6m 25s
-
(Locked)
Bootstrap analysis with R6m 8s
-
(Locked)
checkUsage4m 41s
-
-
-
(Locked)
Use R on the Raspberry Pi7m 32s
-
(Locked)
list2df()4m 28s
-
(Locked)
Introduction to clustering2m 23s
-
(Locked)
Clustering with kmeans6m 57s
-
(Locked)
Clustering with pam and clara6m 23s
-
(Locked)
Understanding silhouette graphs8m 39s
-
(Locked)
Clustering with fanny5m 23s
-
(Locked)
Clustering with hclust5m 12s
-
(Locked)
Clustering with agnes6m 22s
-
(Locked)
Clustering with diana4m 20s
-
(Locked)
cutree and identify with hclust4m 15s
-
(Locked)
Clustering with mona4m 31s
-
(Locked)
Clustering: dist vs. daisy4m 32s
-
(Locked)
-
-
(Locked)
Parameterized R markdown3m 42s
-
(Locked)
Run R on a schedule2m 53s
-
(Locked)
The new forward pipe operator3m 56s
-
(Locked)
Backslash lambda functions5m 24s
-
(Locked)
Dist() in depth5m 29s
-
(Locked)
Scale()3m 9s
-
(Locked)
toJSON4m 6s
-
(Locked)
fromJSON3m 48s
-
(Locked)
Validate JSON2m 28s
-
(Locked)
Plotmath and expression2m 24s
-
(Locked)
Run R in batch mode5m 40s
-
(Locked)
Explore music3m 49s
-
(Locked)
BEEP2m 3s
-
(Locked)
-
-
install.packages4m 27s
-
old.packages, new.packages, and update.packages2m 44s
-
(Locked)
library and require5m 32s
-
(Locked)
Excel in R: SUM5m 51s
-
(Locked)
Excel in R: IF6m 12s
-
(Locked)
Excel in R: LOOKUP5m 17s
-
(Locked)
Excel in R: LEFT and RIGHT4m 15s
-
(Locked)
Excel in R: MATCH4m 50s
-
(Locked)
Excel in R: CHOOSE4m 46s
-
(Locked)
Excel in R: DATE4m 8s
-
(Locked)
Excel in R: DAYS3m 55s
-
(Locked)
Excel in R: FIND and FINDB3m 9s
-
-
-
(Locked)
Excel in R: INDEX2m 28s
-
(Locked)
Excel in R: COUNT4m 5s
-
(Locked)
Excel in R: AVERAGE6m 39s
-
(Locked)
Excel in R: SUMIF and AVERAGEIF5m 17s
-
(Locked)
Excel in R: COUNTIF4m 48s
-
(Locked)
Excel in R: CONCATENATE4m 23s
-
(Locked)
Excel in R: MAX and MIN6m 56s
-
(Locked)
Excel in R: PROPER4m 28s
-
(Locked)
Excel in R: AND6m 58s
-
(Locked)
Excel in R: LEN3m 57s
-
(Locked)
Excel in R: COUNTA6m 28s
-
(Locked)
Excel in R: NETWORKDAYS6m 59s
-
(Locked)
Excel in R: IFERROR6m 27s
-
(Locked)
-
-
(Locked)
Citation2m 39s
-
(Locked)
Vectorize5m 21s
-
(Locked)
Powerpoint from R4m 40s
-
(Locked)
Infix operator2m 30s
-
(Locked)
Kronecker2m 47s
-
(Locked)
Flowcharting3m 47s
-
(Locked)
Glue5m 20s
-
(Locked)
Crayon4m 14s
-
(Locked)
COVID-196m 5s
-
(Locked)
Apexcharter3m 23s
-
(Locked)
Factorial2m 49s
-
(Locked)
Download files5m 58s
-
(Locked)
Choose3m 3s
-
(Locked)
-
-
(Locked)
Beta and gamma3m 20s
-
(Locked)
as.Date()6m 23s
-
(Locked)
as.POSIXlt()5m 20s
-
(Locked)
as.POSIXct()4m 57s
-
(Locked)
Lubridate4m 41s
-
(Locked)
ISOdate()5m 26s
-
(Locked)
system.timezone() and OlsonNames()4m 46s
-
(Locked)
format()4m 28s
-
(Locked)
difftime()5m 29s
-
(Locked)
seq.Date()5m 50s
-
(Locked)
weekdays(), months(), quarters(), Julian()3m 50s
-
(Locked)
-
-
(Locked)
Introduction to Plumber6m 5s
-
(Locked)
Plumber request and response objects6m 43s
-
(Locked)
getwd setwd4m 24s
-
(Locked)
Use Visual Studio Code with R4m 34s
-
(Locked)
Tibbles4m 37s
-
(Locked)
Overview of dplyr4m 52s
-
(Locked)
dplyr: mutate6m 3s
-
(Locked)
dplyr: select4m 18s
-
(Locked)
dplyr: filter2m 27s
-
(Locked)
dplyr: slice and friends2m 59s
-
(Locked)
dplyr: summarise2m 55s
-
(Locked)
dplyr: arrange1m 43s
-
(Locked)
dplyr: group_by2m 34s
-
(Locked)
-
-
(Locked)
dbplyr translates R to SQL5m 14s
-
(Locked)
dplyr: pull4m 41s
-
(Locked)
dplyr: joins3m 50s
-
(Locked)
R7 OOP: Introduction6m 7s
-
(Locked)
R7 OOP: Properties4m 27s
-
(Locked)
R7 OOPS: Property getters and setters5m 38s
-
(Locked)
R7 OOPS: Validators3m 22s
-
(Locked)
R7 OOP: Class Inheritance3m 36s
-
(Locked)
R7 OOP: Generics and Methods6m 39s
-
(Locked)
Python with RStudio5m 12s
-
(Locked)
Animating plots3m 1s
-
(Locked)
Animating ggplot4m 3s
-
(Locked)
Introduction to Quarto6m 50s
-
(Locked)