Get startedGet started for free

Data Utilities

R features a bunch of functions to juggle around with data structures::

  • seq(): Generate sequences, by specifying the from, to, and by arguments.
  • rep(): Replicate elements of vectors and lists.
  • sort(): Sort a vector in ascending order. Works on numerics, but also on character strings and logicals.
  • rev(): Reverse the elements in a data structures for which reversal is defined.
  • str(): Display the structure of any R object.
  • append(): Merge vectors or lists.
  • is.*(): Check for the class of an R object.
  • as.*(): Convert an R object from one class to another.
  • unlist(): Flatten (possibly embedded) lists to produce a vector.

Remember the social media profile views data? Your LinkedIn and Facebook view counts for the last seven days have been pre-defined as lists.

This exercise is part of the course

Intermediate R

View Course

Exercise instructions

  • Convert both linkedin and facebook lists to a vector, and store them as li_vec and fb_vec respectively.
  • Next, append fb_vec to the li_vec (Facebook data comes last). Save the result as social_vec.
  • Finally, sort social_vec from high to low. Print the resulting vector.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# The linkedin and facebook lists have already been created for you
linkedin <- list(16, 9, 13, 5, 2, 17, 14)
facebook <- list(17, 7, 5, 16, 8, 13, 14)

# Convert linkedin and facebook to a vector: li_vec and fb_vec



# Append fb_vec to li_vec: social_vec


# Sort social_vec
Edit and Run Code