wurmlab / SBC361-programming-in-R

QMUL's programming in R course
https://wurmlab.github.io/SBC361-programming-in-R/practical1.html
5 stars 11 forks source link

prac 2 q7 and q8 and stupid to the point of being useless... #14

Closed roddypr closed 3 years ago

EmelineFavreau commented 7 years ago

What about: Q7: Write a loop that iterates over the colours red, green, blue, yellow, orange, purple, pink and prints out the index (ie the position of the colour in the vector) and the colour itself. This should be done in human-readable format (i.e. "the colour red is in position 1 in the vector", "the colour blue is in position 2 in the vector, and so on). Make sure to indent the code appropriately between the curly brackets.

colour_vec <- c("red", "green", "blue", "yellow", "orange", "purple", "pink")
for (index in 1:length(colour_vec)) {
  colour <- colour_vec[index]
  print(paste0("the colour ", colour, " is in position ", index ))
}
yannickwurm commented 7 years ago

Lets please avoid the word index and use 'position' instead. Also:

yannickwurm commented 7 years ago

But I agree your proposed exercise is 100x more relevant than the example. An alternative (or perhaps a followup) might be to access data from a table? (e.g. a table of the most common ants, or the best universities, or the most highly cited papers)

EmelineFavreau commented 7 years ago

Q7 is now about looping through colours. The attentive students will be able to recognise the code from line 172 and modify it slightly.