R tutorials Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Three ways of checking you loaded data correctly

A

str(…)

summary(…)

fix(…)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the multiplication operator in R?

A

*

So 6x7=42 in R would be:

>6*7

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

a) What is a boolean value?
b) If you wanted to ask R whether 3 was less than 4, how would you do it?
c) If you wanted to ask R whether 2+2 was equal to 5, how would you do it?

A

a) In computer science, the Boolean data type is a data type, having two values (usually denoted true and false), intended to represent the truth values of logicand Boolean algebra. It is named after George Boole, who first defined an algebraic system of logic in the mid 19th century. The Boolean data type is primarily associated with conditional statements, which allow different actions and change control flow depending on whether a programmer-specified Boolean conditionevaluates to true or false. It is a special case of a more general logical data type; logic does not always have to be Boolean.
b) < 3<4

[1] TRUE

c) > 2+2==5

[1] FALSE

NB: note that you need a double-equals sign to check whether two values are equal - a single-equals sign won’t work

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you store values into a variable in R?

A

variable<-

For example, to make x = 42:

x<-42

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the division operator in R?

A

/

For example, if x<-42,

< x/2

[1] 21

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you call a function in R?

A

You call a function by typing its name, followed by one or more arguments to that function in parenthesis. Let’s try using the sum function, to add up a few numbers. Enter:

> sum(1,3,5)

[1] 9

Some arguments have names. For example, to repeat a value 3 times, you would call the rep function and provide its times argument:

> rep(“yo ho!”, times = 3)

[1] “yo ho!” “yo ho!” “yo ho!”

Try calling the sqrt function to get the square root of 16.

> sqrt(16)

[1] 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If you don’t know what a given function means/is for/how to use it, how can you get R to help you?

A

help(functionname) brings up help for the given function.

example(functionname) brings up examples of usage for the given function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you figure out what a variable stands for?

A

You can print the value of a variable at any time just by typing its name in the console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you assign a TRUE logical value to a variable x?

A

To assign the TRUE logical value to x:

> x<-TRUE

[1] TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you call a function?

A

You call a function by typing its name, followed by one or more arguments to that function in parenthesis. Examples:

> sum(1,3,5)

[1] 9

Some arguments have names. For example, to repeat a value 3 times, you would call the rep function and provide its times argument:

rep(“Yo ho!”, times = 3)
[1] “Yo ho!” “Yo ho!” “Yo ho!”

Using the sqrt function to get the square root of 16.
> sqrt(16)

[1] 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Files and running scrips:

How would you list the files in the current directory from within R?

How do you run a pre-made script?

A

Typing commands each time you need them only works for short scripts, of course. R commands can also be written in plain text files (with a “.R” extension, by convention) for executing later. You can run them directly from the command line, or from within a running R instance.

We’ve stored a couple sample scripts for you. You can list the files in the current directory from within R, by calling the list.files function. Try it now:

> list.files()

[1] “bottle1.R” “bottle2.R”

To run a script, pass a string with its name to the source function. Try running the “bottle1.R” script:

> source(“bottle1.R”)

[1] “This be a message in a bottle1.R!”

Now try running “bottle2.R”:

> source(“bottle2.R”)

[1] “Will ye be me pen pal?”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a vector in R?

A

A vector is simply a list of values. R relies on vectors for many of its operations, including basic plots. A vector’s values can be numbers, strings, logical values, or any other type, as long as they’re all the same type.

Try creating a vector of numbers, like this:

> c(4,7,9)

[1] 4 7 9

The c function (c is short for Combine) creates a new vector by combining a list of values.

Now try creating a vector with strings:

> c(‘a’,’b’,’c’)

[1] “a” “b” “c”

Vectors cannot hold values with different modes (types). Try mixing modes and see what happens:

> c(1, TRUE, “three”)

[1] “1” “TRUE” “three”

All the values were converted to a single mode (characters) so that the vector can hold them all.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are two ways of creating a sequence vector?

Visualise making a vector with values from 5 though 9 in two different ways. And from 9 down to five. And from 5 to 9 with increments of 0.5.

A

Sequence Vectors

If you need a vector with a sequence of numbers you can create it with

  • start:end notation
  • seq function

So to make a vector with values from 5 through 9:

> 5:9

[1] 5 6 7 8 9

A more versatile way to make sequences is to call the seq function.

> seq(5,9)

[1] 5 6 7 8 9

seq also allows you to use increments other than 1. Try it with steps of 0.5:

> seq(5,9,0.5)

[1] 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0

Now try making a vector with integers from 9 down to 5:

> 9:5

[1] 9 8 7 6 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly