Video není dostupné.
Omlouváme se.
R tutorial - Subsetting your Vectors in R
Vložit
- čas přidán 18. 08. 2024
- Understand thanks to this short video how you can subset your vectors in R.
Join DataCamp today, and start our interactive intro to R programming tutorial for free: www.datacamp.c...
During the previous videos, you have created and worked with vectors of different types. You have even computed their length, and seen how to operate with them on a basic level. A last important element to make your experience with vectors complete is vector subsetting. As the name reveals, it basically comes down to selecting parts of your vector to end up with a new vector, which is a subset of the original vector.
Remember the `remain` vector that we built in one of the previous videos? Here it is again, as a named vector.
Suppose you know want to select the first element from this vector, corresponding to the number of spades that are left. You can use square brackets for this. We write remain, open brackets, 1, close brackets.
The number one inside the square brackets indicates that you want to get the first element from the `remain` vector. The result is again a vector, because a single number is actually a vector of length 1. This new vector contains the number 11. The name of the only element, `spades`, is conveniently kept. If you instead wanted to select the third element, corresponding to the remaining diamonds, you could code remain followed by 3 in square brackets.
This was subsetting using an index, but if you're dealing with named vectors, you can also use the names to perform the selection. Instead of using the index 1 to select the first element, you can use the name `spades`. You type remain followed by spades as a character string inside square brackets.
The result is exactly the same as using the numeric index 1. Can you tell how you can refer to the third element in the vector with a name? You've probably figured that you simply have to type diamonds inside square brackets this time.
Suppose now you want to select the elements in the vector that give the remaining spades and clubs in a one-liner, and store them in a new variable, remain_black. Instead of using a single number inside the square brackets, you can use a vector to specify which indices you want to select. Because spades are at index 1 and clubs at index 4, you use vector containing 1 and 4 inside the square brackets.
How the resulting vector is ordered depends on the order of the indices inside the selection vector. If you change c 1 4 to c 4 1, you will get a vector where the clubs come first.
Of course, subsetting multiple elements can also be done by using names, at least if you're dealing with a named vector. To get the same result as the command above, we write remain, open brackets, then a vector containing the character strings "clubs" and "spades".
There's yet another way to subset vectors, that's specifically useful if you want to select all the elements from a vector, except one. Suppose you want to create a vector that contains all the information that's in the remain vector, except for the spades count. You can write remain, open brackets, minus 1, close brackets.
This command removes the first index from the remain vector. Of course, you can also remove multiple elements, like this for example.
This minus operator does not work with names though. This command, for example, throws an error.
These subsetting techniques will enable you to select only the elements of interest from your vector and continue your analyses with these. Before I unleash you to the last set of exercises of this chapter, I want to talk about one last way to perform vector subsetting: using a logical vector.
To do this, you typically use a logical vector that has the same length as the vector you try to subset. The elements for which the corresponding value in the selecting vector is TRUE, will be kept in the subset. The vector elements that correspond to FALSE, will not be kept. Let's try to select the second and fourth element from the `remain` vector using a logical vector. To this end, we construct a vector containing FALSE, TRUE, FALSE and TRUE, and put it inside the square brackets.
Of course, you could also have created a new vector first and then use it to perform the selection, like this.
Now, you might expect that R throws an error if you try to use a logical vector that is shorter than the vector on which you want to perform subsetting. Trying this out shows a different reality. Suppose you use a vector containing only two logicals instead of a 4
No error whatsoever! That's because R performs something called 'recycling'. R is smart enough to see that the vector of logicals you passed it is shorter than the `remain` vector, so it repeats the contents of the vector until it has the same length as `remain`.
It is great, that you cover every exceptions in this course , like in 4:35 .... it was exactly what i was thinking
The videos are very helpful, we want more!
THANK YOU VERY MUCH!!!
Can we have more videos from this guy he is so good !
Supper good
great videos thanks for awesome explain
I am curious.....why does R perform recycling when the vector used to subset is smaller than the length of the original vector.....but not in the case where we use for example [1] (which is also a vector of length 1 that could potentially be recycled to give all the elements of the original vector...but it only gives the first)???.. or does that apply to logical vectors only?
where could i get the exercise questions??
If remain is already a vector that uses 'c' to concatenate the elements, why do I need to use c again in remain_black
Is it because I need to concatenate 1,4 using c as well?
I realize that you 4 months have passed ,but if anyone is still wondering:
the value between the square brackets is expecting a vector of one of three kinds:
1) the index of the vector element (1 , 2 , 3 , 4 ....etc)
2) the names of a named vector
3) logical (TRUE , FALSE) values corresponding to the number of elements in the vector
if you want to create a vector of length 1 ( if you want to choose just one element) you don't need c() since a vector of length 1 doesn't have any combined data so vector_name[ 1 ] will work , however if you would want to choose 1,2, and 3 you need to combine the values (just like vector creation) ,so you will do vector_name[ c(1 , 2 , 3)]
now something not mentioned in the video is if you want to have successive elements ( 1,2,3 or 4,5,6,7 ) you can use a shortcut of vector_name[1:3] or [4:6] ...etc using this shortcut doesn't require the c() because R understands what you want to do and acts as if you ran vector_name[ c(1,2,,4)]
provid the statistics video in R tools
CAN YOU GET CERTIFICATION FOR R IN YOUR COURSE?
i am student for statistics