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.

note that if you don’t have the Rcompression package, you can’t open Matlab files above the version 6<br />

<strong>for</strong>mat. So, if like me you’ve got a recent version of Matlab, <strong>and</strong> don’t have the Rcompression package,<br />

you’ll need to save your files using the -v6 flag <strong>other</strong>wise R can’t open them.<br />

Oh, <strong>and</strong> Octave users? The <strong>for</strong>eign package contains a read.octave() comm<strong>and</strong>. Just this once, the<br />

world makes life easier <strong>for</strong> you folks than it does <strong>for</strong> all those cashed-up swanky Matlab bastards.<br />

7.9.5 Saving <strong>other</strong> kinds of data<br />

Given that I talked extensively about how to load data from non-R files, it might be worth briefly<br />

mentioning that R is also pretty good at writing data into <strong>other</strong> file <strong>for</strong>mats besides it’s own native<br />

ones. I won’t discuss them in this book, but the write.csv() function can write CSV files, <strong>and</strong> the<br />

write.<strong>for</strong>eign() function (in the <strong>for</strong>eign package) can write SPSS, Stata <strong>and</strong> SAS files. There are also<br />

a lot of low level comm<strong>and</strong>s that you can use to write very specific in<strong>for</strong>mation to a file, so if you really,<br />

really needed to you could create your own write.obscurefiletype() function, but that’s also a long way<br />

beyond the scope of this book. For now, all that I want you to recognise is that this capability is there<br />

if you need it.<br />

7.9.6 Arewedoneyet?<br />

Of course not. If I’ve learned nothing else about R it’s that you’re never bloody done. This listing<br />

doesn’t even come close to exhausting the possibilities. Databases are supported by the RODBC, DBI, <strong>and</strong><br />

RMySQL packages among <strong>other</strong>s. You can open webpages using the RCurl package. Reading <strong>and</strong> writing<br />

JSON objects is supported through the rjson package. And so on. In a sense, the right question is not<br />

so much “can R do this?” so much as “whereabouts in the wilds of CRAN is the damn package that does<br />

it?”<br />

7.10<br />

Coercing data from one class to an<strong>other</strong><br />

Sometimes you want to change the variable class. This can happen <strong>for</strong> all sorts of reasons. Sometimes<br />

when you import data from files, it can come to you in the wrong <strong>for</strong>mat: numbers sometimes get<br />

imported as text, dates usually get imported as text, <strong>and</strong> many <strong>other</strong> possibilities besides. Regardless of<br />

how you’ve ended up in this situation, there’s a very good chance that sometimes you’ll want to convert<br />

a variable from one class into an<strong>other</strong> one. Or, to use the correct term, you want to coerce the variable<br />

from one class into an<strong>other</strong>. Coercion is a little tricky, <strong>and</strong> so I’ll only discuss the very basics here, using<br />

a few simple examples.<br />

Firstly, let’s suppose we have a variable x that is supposed to be representing a number, but the data<br />

file that you’ve been given has encoded it as text. Let’s imagine that the variable is something like this:<br />

> x class(x) # what class is it?<br />

[1] "character"<br />

Obviously, if I want to do calculations using x in its current state, R is going to get very annoyed at<br />

me. It thinks that x is text, so it’s not going to allow me to try to do mathematics using it! Obviously,<br />

we need to coerce x from character to numeric. We can do that in a straight<strong>for</strong>ward way by using the<br />

as.numeric() function:<br />

- 241 -

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

Saved successfully!

Ooh no, something went wrong!