Introduction to R for Data Science (Part Two)

This is the second introduction to R. This will cover matrix, matrix operations, factor matrices, and more.

Ivan Huang
2 min readMar 19, 2023

*Originally published on my Substack. This is just a part of the article.

PS: Please read ‘Introduction to R for Data Science (Part One)’ before reading this one. This is a continued version of part one.

Part one: Introduction to R for Data Science (Part One)

Vector Indexing and Slicing

We can use bracket notation to index and access individual elements from a vector

In the figure above, I use v1[2] to select the second number from v1. This would give me 200. I’ve also done it with v2[3] which gave me a “c”. Just remember that if you want to access individual elements, you would use a bracket.

In order to get multiple vectors, we would add a c after the bracket.

This would allow us to get multiple values as you can see above.

Slicing is where you can grab a continuous slice of vector.

Here I have assigned a new value. The colon(:) is where you would stop. So I would start at 2 and end at 4, which then it gave me 2, 3, and 4. Same thing as v[7:10], it would give me 7, 8, 9, 10.

We can also assign characters with numeric as we mentioned before. The difference is that instead of saying v[2], we can also say v[‘b’], which would give us the same result. To get multiple vectors, we can do the same before by using v[c(‘c’,’d’,a’)].

--

--