06.09.2021 Views

Learning Statistics with R - A tutorial for psychology students and other beginners, 2018a

Learning Statistics with R - A tutorial for psychology students and other beginners, 2018a

Learning Statistics with R - A tutorial for psychology students and other beginners, 2018a

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

As you can see, specifying the arguments by name involves a lot more typing, but it’s also a lot easier<br />

to read. Because of this, the comm<strong>and</strong>s in this book will usually specify arguments by name, 18 since that<br />

makes it clearer to you what I’m doing. However, one important thing to note is that when specifying<br />

the arguments using their names, it doesn’t matter what order you type them in. But if you don’t use<br />

the argument names, then you have to input the arguments in the correct order. In <strong>other</strong> words, these<br />

three comm<strong>and</strong>s all produce the same output...<br />

> round( 3.14165, 2 )<br />

> round( x = 3.1415, digits = 2 )<br />

> round( digits = 2, x = 3.1415 )<br />

but this one does not...<br />

> round( 2, 3.14165 )<br />

How do you find out what the correct order is? There’s a few different ways, but the easiest one is to<br />

look at the help documentation <strong>for</strong> the function (see Section 4.12). However, if you’re ever unsure, it’s<br />

probably best to actually type in the argument name.<br />

Okay, so that’s the first thing I said you’d need to know: argument names. The second thing you<br />

need to know about is default values. Notice that the first time I called the round() function I didn’t<br />

actually specify the digits argument at all, <strong>and</strong> yet R somehow knew that this meant it should round to<br />

the nearest whole number. How did that happen? The answer is that the digits argument has a default<br />

value of 0, meaning that if you decide not to specify a value <strong>for</strong> digits then R will act as if you had<br />

typed digits = 0. This is quite h<strong>and</strong>y: the vast majority of the time when you want to round a number<br />

you want to round it to the nearest whole number, <strong>and</strong> it would be pretty annoying to have to specify<br />

the digits argument every single time. On the <strong>other</strong> h<strong>and</strong>, sometimes you actually do want to round to<br />

something <strong>other</strong> than the nearest whole number, <strong>and</strong> it would be even more annoying if R didn’t allow<br />

this! Thus, by having digits = 0 as the default value, we get the best of both worlds.<br />

3.6<br />

Letting Rstudio help you <strong>with</strong> your comm<strong>and</strong>s<br />

Time <strong>for</strong> a bit of a digression. At this stage you know how to type in basic comm<strong>and</strong>s, including how to<br />

use R functions. And it’s probably beginning to dawn on you that there are a lot of R functions, all of<br />

which have their own arguments. You’re probably also worried that you’re going to have to remember<br />

all of them! Thankfully, it’s not that bad. In fact, very few data analysts b<strong>other</strong> to try to remember all<br />

the comm<strong>and</strong>s. What they really do is use tricks to make their lives easier. The first (<strong>and</strong> arguably most<br />

important one) is to use the internet. If you don’t know how a particular R function works, Google it.<br />

Second, you can look up the R help documentation. I’ll talk more about these two tricks in Section 4.12.<br />

But right now I want to call your attention to a couple of simple tricks that Rstudio makes available to<br />

you.<br />

18 The two functions discussed previously, sqrt() <strong>and</strong> abs(), both only have a single argument, x. SoIcouldhavetyped<br />

something like sqrt(x = 225) or abs(x = -13) earlier. The fact that all these functions use x as the name of the argument<br />

that corresponds the “main” variable that you’re working <strong>with</strong> is no coincidence. That’s a fairly widely used convention.<br />

Quite often, the writers of R functions will try to use conventional names like this to make your life easier. Or at least<br />

that’s the theory. In practice it doesn’t always work as well as you’d hope.<br />

- 54 -

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!