MulaiMulai sekarang secara gratis

Mix it up with control flow

Let's return to the LinkedIn profile views data, stored in a vector linkedin. In the first exercise on for loops you already did a simple printout of each element in this vector. A little more in-depth interpretation of this data wouldn't hurt, right? Time to throw in some conditionals! As with the while loop, you can use the if and else statements inside the for loop.

Latihan ini adalah bagian dari kursus

Intermediate R

Lihat Kursus

Petunjuk latihan

Add code to the for loop that loops over the elements of the linkedin vector:

  • If the vector element's value exceeds 10, print out "You're popular!".
  • If the vector element's value does not exceed 10, print out "Be more visible!"

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# The linkedin vector has already been defined for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)

# Code the for loop with conditionals
for (li in linkedin) {
  if ( ) {
    
  } else {
    
  }
  print(li)
}
Edit dan Jalankan Kode