R stuff Flashcards

1
Q

remove a variable x

A

rm(x)

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

special constant for:

missing or undefined data

A

NA

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

special constant for:

empty object

A

NULL

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

special constant for:

positive / negative infinity?

A

Inf / -Inf

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

special constant for:

results that cannot be reasonably defined

A

NaN

(not a number)

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

Check if a number eg x is finite?

A

is.finite(x)

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

How to combine Values into a Vector or List?

sio

A

c: Combine Values into a Vector or List

eg

c(1,7:9)
c(1:5, 10.5, “next”)

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

what are the three common tasks that c() can be used for?

What does c stand for?

A
  1. Create a vector.
  2. Concatenate multiple vectors.
  3. Create columns in a data frame.

c stands for combine

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

Check if something:

  • is (not) a number?
  • is NULL?
A
  • is.nan()
  • is.null()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Make a vector that holds true, true, false

A
  • c(TRUE, TRUE, FALSE)
  • c(T, T, F)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

v4 <- c(v1,v2,v3,”boo”)

What type will the elements be?

A

all strings

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

Make a vector with the numbers from 1 to 7

A

v <- 1:7 # same as c(1,2,3,4,5,6,7)

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

Make a vector with 0 repeated 77 times

A

v <- rep(0, 77) # repeat zero 77 times: v is a vector of 77 zeroes

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

Make a vector with the even numbers from 10 to 20

A

v <- seq(10,20,2) # sequence: numbers between 10 and 20, in jumps of 2

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

Make a vector with 1,2,3,1,2,3

A

v <- rep(1:3, times=2) # Repeat 1,2,3 twice

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

Make a vector with the numbers 1,1,2,2, … up to 10

A

v <- rep(1:10, each=2) # Repeat each element twice

17
Q

check the length of vector v?

A

length(v)

18
Q

How can you add the elements of two vectors together?

A

eg
v1 <- 1:5 # 1,2,3,4,5
v2 <- rep(1,5) # 1,1,1,1,1

element wise addition:
v1 + v2

19
Q

How can you

  • add 1 to each element of a vector v1?
  • multiply each element by 2?
A
  • v1 + 1
  • v1 * 2
20
Q

Mathematical operations

Consider a vector v. How can you get the

  • sum of all elements?
  • mean of all elements?
  • standard deviation?
  • correlation between v and v*5?
A
  • sum(v)
  • mean(v)
  • sd(v)
  • cor(v, v*5)
21
Q

Logical operations - consider vectors v1 and v2:

compare each element in v1 to corresponding elements in v2 / to the number 2, and return a logical vector

A

v1 < v2 / v1 < 2
v1 > v2
v1 >= v2

v1 == v2
v1 != v2

22
Q

Logical operations - consider vectors v1 and v2:

Generate a logical vector which checks if each element in v1 is greater than 2 OR the corresponding element in v2 is greater than 0

A

(v1>2) | (v2>0) # | is the boolean OR, returns a vector (brackets optional)

If either of the statements are true –> TRUE
if neither is true –> FALSE

23
Q

Logical operations - consider vectors v1 and v2:

Generate a logical vector which checks if each element in v1 is greater than 2 AND the corresponding element in v2 is greater than 0

A

v1>2 & v2>0
(v1>2) & (v2>0)

24
Q

How do you access the ith element of a vector v?

A

v[i]

25
Q

How do you access the 2nd, 3rd, and 4th elements of a vector v?

A

v[2:4]

26
Q

How can you access the 1st and 3rd elements of a vector v (that has 5 elements)?

A

v[c(1,3)]
v[c(T,F,T,F,F)]

27
Q

Where does indexing start for R?

A

from 1 !

28
Q
A