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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

who()<br />

-- Name -- -- Class -- -- Size --<br />

speaker character 10<br />

speech.by.char list 3<br />

utterance character 10<br />

Now we use the importList() function to copy all of the variables <strong>with</strong>in the speech.by.char list:<br />

> importList( speech.by.char )<br />

Names of variables to be created:<br />

[1] "makka.pakka" "tombliboo" "upsy.daisy"<br />

Create these variables? [y/n]<br />

Because the importList() function is attempting to create new variables based on the names of the<br />

elements of the list, it pauses to check that you’re okay <strong>with</strong> the variable names. The reason it does this<br />

is that, if one of the to-be-created variables has the same name as a variable that you already have in<br />

your workspace, that variable will end up being overwritten, so it’s a good idea to check. Assuming that<br />

you type y, it will go on to create the variables. Nothing appears to have happened, but if we look at our<br />

workspace now:<br />

> who()<br />

-- Name -- -- Class -- -- Size --<br />

makka.pakka character 4<br />

speaker character 10<br />

speech.by.char list 3<br />

tombliboo character 2<br />

upsy.daisy character 4<br />

utterance character 10<br />

we see that there are three new variables, called makka.pakka, tombliboo <strong>and</strong> upsy.daisy. Notice that the<br />

importList() function has converted the original character strings into valid R variable names, so the<br />

variable corresponding to "makka-pakka" is actually makka.pakka. 10 Nevertheless, even though the names<br />

can change, note that each of these variables contains the exact same in<strong>for</strong>mation as the original elements<br />

of the list did. For example:<br />

> makka.pakka<br />

[1] "pip" "pip" "onk" "onk"<br />

7.5<br />

Extracting a subset of a data frame<br />

In this section we turn to the question of how to subset a data frame rather than a vector. To that end,<br />

the first thing I should point out is that, if all you want to do is subset one of the variables inside the<br />

data frame, then as usual the $ operator is your friend. For instance, suppose I’m working <strong>with</strong> the itng<br />

data frame, <strong>and</strong> what I want to do is create the speech.by.char list. I can use the exact same tricks<br />

that I used last time, since what I really want to do is split() the itng$utterance vector, using the<br />

itng$speaker vector as the grouping variable. However, most of the time what you actually want to do<br />

10 You can do this yourself using the make.names() function. In fact, this is itself a h<strong>and</strong>y thing to know about. For<br />

example, if you want to convert the names of the variables in the speech.by.char list into valid R variable names, you could<br />

use a comm<strong>and</strong> like this: names(speech.by.char)

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

Saved successfully!

Ooh no, something went wrong!