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.

<strong>for</strong> ages, you figure out some really clever tricks that can be used to solve it. Then, in September,<br />

you get h<strong>and</strong>ed a really similar problem. You can sort of remember what you did, but not very<br />

well. You’d like to have a clean record of what you did last time, how you did it, <strong>and</strong> why you did<br />

it the way you did. Something like that would really help you solve this new problem.<br />

• It’s hard to do anything except the basics. There’s a nasty side effect of these problems. Typos<br />

are inevitable. Even the best data analyst in the world makes a lot of mistakes. So the chance<br />

that you’ll be able to string together dozens of correct R comm<strong>and</strong>s in a row are very small. So<br />

unless you have some way around this problem, you’ll never really be able to do anything <strong>other</strong><br />

than simple analyses.<br />

• It’s difficult to share your work <strong>other</strong> people. Because you don’t have this nice clean record of what<br />

R comm<strong>and</strong>s were involved in your analysis, it’s not easy to share your work <strong>with</strong> <strong>other</strong> people.<br />

Sure, you can send them all the data files you’ve saved, <strong>and</strong> your history <strong>and</strong> console logs, <strong>and</strong><br />

even the little notes you wrote to yourself, but odds are pretty good that no-one else will really<br />

underst<strong>and</strong> what’s going on (trust me on this: I’ve been h<strong>and</strong>ed lots of r<strong>and</strong>om bits of output from<br />

people who’ve been analysing their data, <strong>and</strong> it makes very little sense unless you’ve got the original<br />

person who did the work sitting right next to you explaining what you’re looking at)<br />

Ideally, what you’d like to be able to do is something like this. . . Suppose you start out <strong>with</strong> a data<br />

set myrawdata.csv. What you want is a single document – let’s call it mydataanalysis.R –thatstoresall<br />

of the comm<strong>and</strong>s that you’ve used in order to do your data analysis. Kind of similar to the R history but<br />

much more focused. It would only include the comm<strong>and</strong>s that you want to keep <strong>for</strong> later. Then, later<br />

on, instead of typing in all those comm<strong>and</strong>s again, you’d just tell R to run all of the comm<strong>and</strong>s that are<br />

stored in mydataanalysis.R. Also, in order to help you make sense of all those comm<strong>and</strong>s, what you’d want<br />

is the ability to add some notes or comments <strong>with</strong>in the file, so that anyone reading the document <strong>for</strong><br />

themselves would be able to underst<strong>and</strong> what each of the comm<strong>and</strong>s actually does. But these comments<br />

wouldn’t get in the way: when you try to get R to run mydataanalysis.R it would be smart enough would<br />

recognise that these comments are <strong>for</strong> the benefit of humans, <strong>and</strong> so it would ignore them. Later on you<br />

could tweak a few of the comm<strong>and</strong>s inside the file (maybe in a new file called mynewdatanalaysis.R) so<br />

that you can adapt an old analysis to be able to h<strong>and</strong>le a new problem. And you could email your friends<br />

<strong>and</strong> colleagues a copy of this file so that they can reproduce your analysis themselves.<br />

In <strong>other</strong> words, what you want is a script.<br />

8.1.2 Our first script<br />

Okay then. Since scripts are so terribly awesome, let’s write one. To do this, open up a simple text<br />

editing program, like TextEdit (on a Mac) or Notebook (on Windows). Don’t use a fancy word processing<br />

program like Microsoft Word or OpenOffice: use the simplest program you can find. Open a new text<br />

document, <strong>and</strong> type some R comm<strong>and</strong>s, hitting enter after each comm<strong>and</strong>. Let’s try using x

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

Saved successfully!

Ooh no, something went wrong!