R Lecture Week3 Flashcards

1
Q

whats wrong with this code and why

A

the single pipe operator is vectorised |
but the if statment is not vectoorised (it will only look at the first element that we get out)

so we should here use the double pipe || operator
we use || to be very clear that we are only taking in a single true or false

for if statements we should remeber to use && and ||

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

why with this ifelse function do we only use the | single

A

because ifelse is vectorised so we are wanting to return a respose for all 4 elements in the vector

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

1how to generate random numbers between 1 and 1000

2how to make sure you get the same random numbers

A
  1. sample(x = 1:1000, size = 100)

2. set.seed(5)

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

how to check if something is NOT na

A

!is.na(NA)

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

if( !is.na(as.numeric(input_miles))))

explain that line of code

A
  • tries convert to numeric. if it can true. if it cant, NA.

then check value if its NA. run the code if its not NA

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