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.

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

[1] "numeric"<br />

> x + 1 # hey, addition works!<br />

[1] 101<br />

Not surprisingly, we can also convert it back again if we need to. The function that we use to do this is<br />

the as.character() function:<br />

> x class(x) # check the class:<br />

[1] "character"<br />

However, there’s some fairly obvious limitations: you can’t coerce the string "hello world" intoanumber<br />

because, well, there’s isn’t a number that corresponds to it. Or, at least, you can’t do anything useful:<br />

> as.numeric( "hello world" ) # this isn’t going to work.<br />

[1] NA<br />

Warning message:<br />

NAs introduced by coercion<br />

In this case R doesn’t give you an error message; it just gives you a warning, <strong>and</strong> then says that the data<br />

is missing (see Section 4.6.1 <strong>for</strong> the interpretation of NA).<br />

That gives you a feel <strong>for</strong> how to change between numeric <strong>and</strong> character data. What about logical data?<br />

To cover this briefly, coercing text to logical data is pretty intuitive: you use the as.logical() function,<br />

<strong>and</strong> the character strings "T", "TRUE", "True" <strong>and</strong> "true" all convert to the logical value of TRUE. Similarly<br />

"F", "FALSE", "False", <strong>and</strong>"false" all become FALSE. All <strong>other</strong> strings convert to NA. When you go back<br />

the <strong>other</strong> way using as.character(), TRUE converts to "TRUE" <strong>and</strong> FALSE converts to "FALSE". Converting<br />

numbers to logicals – again using as.logical() – is straight<strong>for</strong>ward. Following the convention in the<br />

study of logic, the number 0 converts to FALSE. Everything else is TRUE. Going back using as.numeric(),<br />

FALSE converts to 0 <strong>and</strong> TRUE converts to 1.<br />

7.11<br />

Other useful data structures<br />

Up to this point we have encountered several different kinds of variables. At the simplest level, we’ve seen<br />

numeric data, logical data <strong>and</strong> character data. However, we’ve also encountered some more complicated<br />

kinds of variables, namely factors, <strong>for</strong>mulas, data frames <strong>and</strong> lists. We’ll see a few more specialised data<br />

structures later on in this book, but there’s a few more generic ones that I want to talk about in passing.<br />

None of them are central to the rest of the book (<strong>and</strong> in fact, the only one we’ll even see anywhere else<br />

is the matrix), but they do crop up a fair bit in real life.<br />

7.11.1 Matrices (<strong>and</strong> arrays)<br />

In various different places in this chapter I’ve made reference to an R data structure called a matrix,<br />

<strong>and</strong> mentioned that I’d talk a bit more about matrices later on. That time has come. Much like a data<br />

frame, a matrix is basically a big rectangular table of data, <strong>and</strong> in fact there are quite a few similarities<br />

- 242 -

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

Saved successfully!

Ooh no, something went wrong!