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.

Two <strong>other</strong> methods that I want to briefly refer to are the rbind() <strong>and</strong> cbind() functions, which will<br />

convert the vectors into a matrix. I’ll discuss matrices properly in Section 7.11.1 but the details don’t<br />

matter too much <strong>for</strong> our current purposes. The cbind() function (“column bind”) produces a very similar<br />

looking output to the data frame example:<br />

> cake.mat1 cake.mat1<br />

cake.1 cake.2<br />

[1,] 100 100<br />

[2,] 80 100<br />

[3,] 0 90<br />

[4,] 0 30<br />

[5,] 0 10<br />

but nevertheless it’s important to keep in mind that cake.mat1 is a matrix rather than a data frame,<br />

<strong>and</strong> so has a few differences from the cake.df variable. The rbind() function (“row bind”) produces a<br />

somewhat different output: it binds the vectors together row-wise rather than column-wise, so the output<br />

now looks like this:<br />

> cake.mat2 cake.mat2<br />

[,1] [,2] [,3] [,4] [,5]<br />

cake.1 100 80 0 0 0<br />

cake.2 100 100 90 30 10<br />

You can add names to a matrix by using the rownames() <strong>and</strong> colnames() functions, <strong>and</strong> I should also<br />

point out that there’s a fancier function in R called merge() that supports more complicated “database<br />

like” merging of vectors <strong>and</strong> data frames, but I won’t go into details here.<br />

7.6.5 Binding multiple copies of the same vector together<br />

It is sometimes very useful to bind together multiple copies of the same vector. You could do this<br />

using the rbind <strong>and</strong> cbind functions, using com<strong>and</strong>s like this one<br />

> fibonacci rbind( fibonacci, fibonacci, fibonacci )<br />

[,1] [,2] [,3] [,4] [,5] [,6]<br />

fibonacci 1 1 2 3 5 8<br />

fibonacci 1 1 2 3 5 8<br />

fibonacci 1 1 2 3 5 8<br />

but that can be pretty annoying, especially if you needs lots of copies. To make this a little easier, the<br />

lsr package has two additional functions rowCopy <strong>and</strong> colCopy that do the same job, but all you have to<br />

do is specify the number of copies that you want, instead of typing the name in over <strong>and</strong> over again. The<br />

two arguments you need to specify are x, the vector to be copied, <strong>and</strong> times, indicating how many copies<br />

should be created: 15<br />

> rowCopy( x = fibonacci, times = 3 )<br />

[,1] [,2] [,3] [,4] [,5] [,6]<br />

15 Note <strong>for</strong> advanced users: both of these functions are just wrappers to the matrix() function, which is pretty flexible in<br />

terms of the ability to convert vectors into matrices. Also, while I’m on this topic, I’ll briefly mention the fact that if you’re<br />

a Matlab user <strong>and</strong> looking <strong>for</strong> an equivalent of Matlab’s repmat() function, I’d suggest checking out the matlab package<br />

which contains R versions of a lot of h<strong>and</strong>y Matlab functions.<br />

- 221 -

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

Saved successfully!

Ooh no, something went wrong!