Vectors

Creating a vector

Let's take a closer look at numericvectors.

Do you remember how to combine different values into one vector?

> x <- c(29,3,17)

Nice! We can use the c() function to combine values into one vector.

Sequences

A lot of times, we might want a list of numbers generated for us. If that's the case, we can use the colon operator : to create so-called sequences.

> my_sequence <- c(10:15)

//Output Below

[1] 10 11 12 13 14 15

Nice! The colon operator will create a sequence by adding 1 until it reaches 15.

Seq()

R also has a function that can create specific sequences: the seq() function. What might the following code print out?

> x <- seq(10,20,by=5)
  • [1] 10 15 20

Yass! The seq() function creates a sequence from 10 to 20. The by=5 part means we increment by 5 to get the next value.

Decimal

What if we want to create a sequence that has the increment size different from 1?

> my_sequence <- seq(2.1,4,by=0.5)

//Output Below

[1] 2.1 2.6 3.1 3.6

Awesome! The seq() function allows us to be very specific with decimal numbers. Here, we used the 0.5 decimal to increment our sequence.

Vector types

An important thing to remember is that R vectors can only contain elements of the same data type.

> my_vector <- c(8,3,11)
> my_vector

//Output Below

[1] 8 3 11

Awesome! To combine different type of elements in R we have to use lists! We'll explore lists a bit later.

Type coercion

We can't combine strings and numbers in one vector, but what about different kinds of numbers, such as integer and decimal?

> y <- c(1.1, 3.14)
> x <- c(y, 555)
> x
  • [1] 1.10 3.14 555.00

Woah, good job! If we add an integer to a decimal vector, R will automatically change it's type to decimal. This is called type coercion.

Psst: type coercion happens when there are values of different types. R converts to a type that works for all cases and doesn't lose any data.

Subsetting

Smaller pieces of our collected data are called subsets. To get a subset, we need to use the square brackets [] and an index of the value we want.

How would we get the 3rd value of a vector?

> x <- c(75,80,83)
> x[3]

//Output Below

[1] 83

Great! The 3rd element is in the 3rd position in the vector.

Psst: R is designed for humans, which is why the index starts at 1, rather than 0 as most programming languages.

Bigger subsets

Remember how we used the colon operator to create sequences? Using :, Let's try selecting the three values in the middle of the vector.

> x <- c(90,85,81,84,84) 
> x[2:4]

//Output Below

[1] 85 81 84

Boom! R makes it super easy to get the values we need.

Name attribute

Vectors can also have attributes, extra information which helps clarify data. The names() function allows us to give a name attribute to each value.

> x <- c(75,80,83)
> days <- c("Mon","Tue","Wed")
> names(x) <- days
> x

//Output Below

Monday   Tuesday Wednesday 
    75        80        83 

Sweet! Notice how both the days and x vectors are of the same length.

Psst: R doesn't number each line when we print out vectors with a name attribute.

Subsetting by name

Now that we've given names to our values, let's try subsetting the value recorded on Wednesday.

> x <- c(75,80,83)
> names(x) <- c("Mon","Tue","Wed")
> x ["Wed"]

//Output Below

Wed
 83

Woop woop! We simply need to type in the word "Wed" as a string and we'll get our result.

Length

Because R is made for large collections of data, it can come in handy to know how many elements a vector has.

Can you guess the name of the function that does that?

> year <- 365
> even_days <- seq(2,year,by=2)
> length(even_days)

//Output Below

[1] 182

Awesome! We can use length() to find out how many elements a vector has.

Hello again

Data can come in many more forms, including words. To store a string in R, we need to use a vector of type character.

> sentence <- "Ay caramba"
> sentence

//Output Below

Ay caramba

Boom! We just created a character vector.

Length

We can also use the length() function on character vectors. What do you think is the length of sentence?

> sentence <- "Bird is the word."
> length(sentence)
  • [1] 1

Wahey! sentence is a character vector with 1 element. The element is made out of 4 words and 17 characters, but that doesn't affect the vector length.

Characters

If we want to measure the number of characters, we can use the nchar() function.

> sentence <- c("Hello", "my name is", "the real")
> nchar(sentence)
  • [1] 5 10 8

Super! The output is the number of characters for each element belonging to the vector.

Adding words

Adding other entries is the same process as with numeric vectors.

> flavors <- c("blueberry","kiwi")
> flavors <- c(flavors, "banana")
> flavors

//Output Below

[1] "blueberry"     "kiwi"       "banana"

See that? We can easily add another entry with the c() function.

Paste

A very handy feature is the paste() function. It lets us combine all of our vector elements into one. We just need to decide on how to link them.

Use the paste() function to link the words with spaces in-between.

> word <- c("We","all","scream","for","ice","cream")
> paste(word,collapse = " ")

//Output Below

[1] "We all scream for ice cream"

Perfect! We can use the paste() function with a character vector and a collapseargument to combine vector elements into one.

Logical

Let's move on to another vector type: logical.

> answers <- c(TRUE,FALSE)
> answers

//Output Below

[1] TRUE FALSE

See that? Logical vectors only have two main values: TRUE or FALSE.

Psst: in R, all vectors can also have a value called NA. We'll get to that soon!

Operators

Logical vectors are usually the result of operations carried out on other vectors. Common logical operators are >,<, and == among others.

How might we check if x is longer than y?

> x <- c(1,2,3,4)
> y <- c(5,6,7)
> length(x) > length(y)

//Output Below

[1] TRUE

Awesome work! We use the > to see if the value on the left is greater than the one on the right.

Operations

What might happen if we checked the vector values instead of their lengths?

> x <- c(517,234,10)
> y <- c(-38,307,10)
> result <- x > y
> result

//Output Below

[1] TRUE FALSE FALSE

Nice! We compared the values of two vectors. we can do this with other logical operators such as > or == as well.

Comparing

Logical vectors can also be a result of comparing character vectors. Let's see if the two vectors have equal values.

> name <- c("Mr.","Bond")
> name2 <- c("Mrs.","Bond")
> result <- name == name2
> result
  • [1] FALSE  TRUE

Excellent! "Mr." and "Mrs." are not the same, so the result is FALSE. The result is a logical vector of the same length as the compared vectors.