# Deep (learning) like Jacques Cousteau – Part 4 – Scalar multiplication

**Embracing the Random | R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

(TL;DR: Multiply a vector by a scalar one element at a time.)

**LaTeX and MathJax warning for those viewing my feed: please view
directly on website!**

We build, we stack, we multiply

Nate Dogg from ‘Multiply’ by Xzibit

Last

time,

we learnt about ** vectors**. Before

that,

we learnt about

**. What happens when we**

`scalars`

**multiply a vector**

by a scalar?

by a scalar

*(I don’t know where I’m going with this diagram…but bear with me!)*

# Today’s topic: Multiplying vectors by scalars

Let’s use our vector from last time.

Let’s pick a **scalar** to multiply it by. I like the number two, so

let’s multiply it by two!

To evaluate this, we perform **scalar multiplication**. That is, we

multiply **each element** of our vector by our scalar. Easy!

More generally, if our vector contains elements

and we multiply it by some scalar , we get:

## How can we perform scalar multiplication in R?

This is easy. It’s what R does by default.

Let’s define our vector, **x**.

```
<span class="n">x</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w">
</span><span class="n">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w">
</span>
```

`## [1] 1 2 3`

Let’s define our scalar, **c**.

```
<span class="n">c</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">2</span><span class="w">
</span><span class="n">print</span><span class="p">(</span><span class="n">c</span><span class="p">)</span><span class="w">
</span>
```

`## [1] 2`

Now, let’s multiply our vector by our scalar.

```
<span class="n">c</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="w">
</span>
```

`## [1] 2 4 6`

Boom! **The power of vectorisation!**

## How does type coercion affect scalar multiplication?

The comments we made in an earlier post about **type coercion** apply

here. Let’s define ** x** as an

**integer vector**.

```
<span class="n">x</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1L</span><span class="p">,</span><span class="w"> </span><span class="m">2L</span><span class="p">,</span><span class="w"> </span><span class="m">3L</span><span class="p">)</span><span class="w">
</span><span class="nf">class</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w">
</span>
```

`## [1] "integer"`

Our scalar ** c** may also look like an integer, but it has been stored

as a

**type, which is our proxy for**

`numeric`

**real numbers**.

```
<span class="n">print</span><span class="p">(</span><span class="n">c</span><span class="p">)</span><span class="w">
</span>
```

`## [1] 2`

```
<span class="nf">class</span><span class="p">(</span><span class="n">c</span><span class="p">)</span><span class="w">
</span>
```

`## [1] "numeric"`

So when we multiply a ** numeric** type by our

**vector, we**

`integer`

get a result in the more general

**type!**

`numeric`

```
<span class="nf">class</span><span class="p">(</span><span class="n">c</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w">
</span>
```

`## [1] "numeric"`

# Conclusion

To multiply a vector by a scalar, simply multiply each element of the vector by the scalar. This is pretty easy, isn’t it?

Let’s learn how to **add** two vectors before we cover **dot products**.

Only then can we **enter the matrix!**

Justin

**leave a comment**for the author, please follow the link and comment on their blog:

**Embracing the Random | R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.