[go: up one dir, main page]

0% found this document useful (0 votes)
6 views2 pages

04 Scalar Variables - en

Uploaded by

Agus Setiawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

04 Scalar Variables - en

Uploaded by

Agus Setiawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

[MUSIC] In the past video, we described how

to use mathematical functions in r. But notice in this example


here I have two plus two, if I run this code I get the answer for
but the answer is not stored anywhere. So in this lecture we're going to talk
about variables in our basically how to store information and
the way to do that is really simple. First you create a variable name,
I'm going to call it x. Not a very creative name but it'll do, then you use this
thing
that looks like an arrow. So it's a less than sign and a hyphen and then I'm going
to put
a value in there of four. It hasn't run yet. So let me run that code down below.
And as I mentioned in a previous video,
when I hit that run button, it's like cutting and pasting that
command down into the console and hitting enter and so on the console
you see x is assigned a value of four. Now what does that mean? Creating a variable
in r is creating,
it's like creating space on the computer. So imagine you have a shoebox and
that's your space on the computer. And then you label that shoebox with
the stuff that you're going to put on it. In this case I named it x. It could be
things like height. So if you have a team roster, this is the list of all the
heights of
all the players, it could be the weights, it could be birthdates,
it could be whatever you want. So you want to label the shoebox and put
like kind of data within that shoe box. Here, I just put the value of four. Also
notice in the top
right of our studio here, you can see in the global
environment types or click on that if it's not
active there's your variable x. And here's the contents of that shoe box,
its our, okay? Now let's create a second variable y. A value of three, okay? And
there, now you can see I've
executed that code here and you can see it in the global
environment up here. I've created a little space for it. And y is equal to a value
of three. Another way you can see the value
of what's inside a variable. I'm in the console here now and
I can just type that variable name, x. And it will respond with four in this
case or y and it responds with three. So I can see the inside of the box that
way, I can see it in the environment. And now I can do some
commands as we noted before. So let's just do x plus y. There we go. We run that
and
x plus y in this case is seven. Which makes sense because x
contained a value of four and y contained the value of three. But notice that the
sum of x and
y is not stored anywhere, so I can create a third variable,
let's call it z. Use the assignment operator which is
the less sand sign hyphen and x plus y. Let's run that line and
you can see now in the top right in the environment section of our studio,
we have created the variable z and at the same time put in
the value of x plus y. So that's how you create
a scalar variable. One thing to note is that
the variables can be of any type. So if you want to put text in there,
you have to put it in quotes. So let's put, I'll just call this
variable t1, for text example one. And I can write hi mom. And there it is and you
can see in
the environment there's hi mom. If I go down here and
type T1, it'll show hi mom. But this is a character string. So what would you
expect if I
did something like x plus t1. Have you tried that? Don't be afraid to eat dinner
but you can see that it's an error because
it's trying to add a text string. String is a bunch of
characters with a numeric. One final note about variable names. There are a number
of ways
you can write variable names. There are almost no limitations on
what you can call a variable, but in terms of style and readability, you want to
pick a style that's
consistent throughout your programs. And one style I like to use
is called lower camel case. So what does that mean? Say you have a column of data
or variable that is something like shoe size, camel case would look like this. Shoe
size. So notice I've capitalized S here and so you can string along a phrase and
then capitalize each word. Some people like to capitalize
the first letter as well, so it looks more like capitalist for shoe size,
something like this, it doesn't matter. Just pick a style and and
stick with it for readability. Also note if you go into
the corporate environment, they might have their own data
variable naming standards. So that's something you should think
about and try to stay consistent with the corporate environment, and
that wraps up scalar variables.

You might also like