Data Utilities
R features a bunch of functions to juggle around with data structures::
seq()
: Generate sequences, by specifying thefrom
,to
, andby
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
Exercise instructions
- Convert both
linkedin
andfacebook
lists to a vector, and store them asli_vec
andfb_vec
respectively. - Next, append
fb_vec
to theli_vec
(Facebook data comes last). Save the result associal_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