Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 44 / 68

Pairing up two vectors (the zip operation)

This lesson covers pairing two vectors with R's Map, with the aim of handling related data together.

Combining two corresponding vectors — one of names, one of scores — element by element is called a zip. In R the Map() function does it.

The sample code combines the elements at matching positions of names_vec and scores_vec with Map(function(n, s) paste0("(", n, ", ", s, ")"), names_vec, scores_vec). Note unlist(pairs, use.names = FALSE), which turns the result into a readable vector.

A common early stumble is that Map() returns a list. To use it as a vector you have to convert with unlist(), and forgetting that leaves you confusingly handling a list.

In professional work, this idea is applied constantly when several related vectors have to be processed together.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run R directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)