$ operator is invalid for atomic vectors
This is a topic that many people are looking for. https://granthamandira.org/ is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, https://granthamandira.org/ would like to introduce to you R – Atomic Vectors. Following along are instructions in the video below:
Hello and welcome to the video covering vectors in R in this video we will first began by looking at four different types of vectors then we will look at some common functions that are used to identify different types of objects. Well also look at indexing a vector and then what happens whenever you combine vectors of different types so lets begin by creating some example vectors and we can see that with x, its going to be double or numeric which are the most commonly used If youre just going to be using some numbers with decimal places involved y is going to be an integer so notice that it was created using a colon. So its a series of numbers where as with x, whenever we created a numeric vector we had to call upon the concatenate or combined function which is why you see the “c” with parentheses and then the number inside next if we take a look at the z vector, we created a logical factor here also using the concatenate function and then inputting TRUE or FALSE in each of those element places and the last vector created is stock.sym which is going to be a vector of characters and these are names of companies that have different stock prices which all these vectors will be a part of whenever we get to the example and actually rename some of the other ones but to keep it simple were just using x, y, and z to be able to manipulate them. Now although the type and size of objects are
shown in the environment window we can also call upon some commonly used functions in R to come up with what type of variable we have or what is the size so for example if I wanted to come up with the type of vector for x I could simply use typeof() and when that is run we can see that it is double which in the Environment window it comes up as numeric, but those are going to be the same thing you can also use the structure function which is str( ). So if I use str(x) you can see that its going to give me what type of object it is so its a numeric as well as how many elements are in it so says 1 through 4 and then it gives me some of the beginning elements of that vector which would be all the same information that youd find in the Environment window anyways. Now if you just simply wanted to call up how long a vector is you can use length so if we called up lenght(x), it would just return 4 and another function is the dimension function which is dim but if I run that on x, which is a vector, youll see that it returns NULL. You should keep in mind that the dimension function should be used for objects other than vectors. The next topic when wed like to take a look at is going to be indexing vectors. So lets recall x is going to be these 4 elements here and lets say that I
wanted to call upon the second element so I want to call up the 53.23; or lets say I didnt know what was, and I just wanted call up the second element. So to do this, you would call upon the object and then use brackets with whatever your index was going to be so I would use x and it will return that second element and lets say I wanted more than one element Well, to do this, I would have to also call upon the concatenate function so if I wanted the first and third element I could simply use the concatenate function to use 1 and 3 then that is gonna be an input as my index into x which you can see returns the first and third element. If I wanted to return a series of elements, Lets say I had a vector of length 100 and I wanted the 80th through the 100th element I wouldnt want to put in 80, 81, 82, 83, 84 all inside the concatenate function and then have to put commas between each one so something more simple lets say we wanted the second through the fourth element of X. To do that you can just put a colon in between the 2 and 4 and index all of those elements in one call much much more simply then use in the concatenate function One other thing is that you could use logicals to also index into a vector. So, for example, if we called upon our logical z to index into x, then you can see
that well with z, if I call it back up, it will allow us to retrieve the first, third, and fourth elements out of x which you could also do by simply saying x minus 2 to remove the second element and it will return the same thing. Now another thing to keep in mind is if you combine vectors of different types then there are some default results which occur. For example, if we were to add x and y x being numeric and y being an integer then notice that, you can see that sum_xy is numeric because by default numeric is going to take the place of integers. Now if instead we add together x and z, which means that we would be adding together numeric and logical, the result would again be numeric so sum_xz, you can see in the Environment window, is indeed numeric and thats because z could also be considered (0,1). So, where Z is true, false, true, true you could think of that is (1,0,1,1) and the result then would end up being as shown in the Environment window. If we were to add integers and logicals together, like in y plus z, you can see it ends up being integer, so again the logical would be viewed as (1,0,1,1) and then add to the integers which were in y, you would just simply be adding 1 to the first third and fourth element in y. And then just returning the second element in y as it was. So by default, numeric is going to replace integer which replaces
we were to add up x and y and z, we could see that sum_xyz is shown in the Environment window as still numeric. And its important to keep these things in mind because if you start to add together these different types, and certain functions are only going to take inputs of a certain type, then just be aware that by combining these vectors of different types, that by default something is going to occur. Now its not really common that youre going to be adding together something like a numerical result with a logical result but it is more common too simply treat logicals as numeric. So lets recall that z was a logical vector: TRUE, FALSE, TRUE, TRUE and what if we were interested in the question “Well how many true elements are there in z?” Well with the simple example, we can clearly see that there are 3 but if it was a much larger vector something of length 1,000, then we could answer this much faster by simply calling upon the sum function. Notice that because its a function, I want to use parentheses. So if we sum(z) notice we get 3 because its treating each of the TRUEs as 1. Or another question might be, “what proportion of z elements are actually TRUE?” if we had this question, then what we could look at is the (sum of z) / (length of z); which in this case would return 3 out of 4. This concludes the video on working with vectors in R. We hope you found this useful, and happy coding!
CADS, Miami University, vectors in R, R programming, R syntax, indexing, RStudio, analytics, vector types
Thank you for watching all the articles on the topic R – Atomic Vectors. All shares of https://granthamandira.org/ are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.