2.2 Work with variables - Video Tutorials & Practice Problems
Video duration:
4m
Play a video:
<v Voiceover>Like any</v> other programming language, R has the facility to store data in a variable. That's often handy, if you wanna use that variable again and again, or if you're working programmatically, and you have a specific multiplier or some option that you want to take on different values at different times. There are a few ways to assign variables in R, the preferred way being the left arrow. So, let's say we want to assign the value two to the variable x. We type in x gets two. Running that, we see over in our environment that x has the value of two. And, that's just using our environment. If we want to actually see it in the console, we go ahead and type in x in the text editor and hit Control + Enter, and we get two back, just as we expected. Another way to assign a variable, though this isn't preferred within the community, is to use the equal sign. Such as y gets five. Running that, we see that y is equal to five. Now, there could be questions as to why we want to use the arrow instead of using the equal sign. This goes back to before R even existed, to the language called S, which R is derived from, and it has something to do with those idiosyncrasies. Either way, the arrows have become the preferred method at large. And as another example, we will show just z gets three. As you can see, it's working more or less as expected. Now, some cool tricks we can do with this is, serial assignment. Assigning multiple variables at the same time. For instance, a gets b, gets seven. We look at that, a is seven, and b is seven. Works just like we want it to. And lastly, there's a more verbose way which is more useful in a programmatic fashion. There's a function called assign which lets you assign a value to a variable. So, it is assign, open parentheses, then inside quotes, the name of the variable you want to assign to, comma, the value you are assigning. When you run that and look at it, j is equal to four. Again, this is not something you would probably do in everyday usage, but once in while, that could be very handy. Sometimes, you're going to want to remove a variable. There can be many reasons why, and fortunately, it's easy to do. Let's say we want to remove j, which we just created. It's as simple as typing rm, stands for remove, then j, the variable you want to remove. After running that, if we enter j, it will say, "Object not found." It's gone now. So, you have to be careful when removing variables, because when they're removed, they're gone. It's also important to remember that R is case-sensitive. So, capitalization really matters. For instance, let's say we have a variable called the variable. And we assign that the value of 17. All is good. We type in the variable. And here's a nice feature about our studio. We've only typed in part of the word. If you hit the tab key, it finishes it for us. Very nice feature. So if we run this line, we see it's 17. If we were to use all capitals, the variable, and run that, "Object not found." That's because we used different cases for the letters and that makes a big difference. So, assigning and retrieving values is quite simple using either the arrow symbol or the equal sign. And removing them is as simple as rm. And case matters.