Vector Magic: The Alchemy of Data

Vector Magic: The Alchemy of Data


6 min read

Data manipulation is an essential skill for any data scientist or analyst. In the world of data science, vectors play a crucial role in data manipulation. Vectors are sequences of data elements, and understanding how to work with them can be magical. In this blog, we'll explore some vector operations in R, a powerful language for data analysis, and see how these operations can transform your data.

Quick Check: Are You Greater Than 12?

Let's start with a simple example. Suppose you have a vector x containing some numbers, and you want to check which elements are greater than 12. In R, you can do this with a straightforward comparison:

x <- c(8, 10, 12, 7, 14, 16, 2, 4, 9, 19, 20, 3, 6)
x > 12

When you run this code, R checks each element of x and generates a new vector with TRUE and FALSE values, indicating whether each element is greater than 12.



The result is a new vector of Boolean values, telling you which elements meet the condition. This is the foundation of data manipulation with vectors: operations are performed on each element separately, resulting in a new vector.

Filtering Magic: Where Vectors Dance

Now that we know how to check conditions, we can use this information to filter our data. You can create a new vector, y, that contains only the elements of x greater than 12:

y <- x[x > 12]


[1] 14 16 19 20

Here, we placed the logical condition x > 12 inside the square brackets, and it filtered the elements of x that met the condition into y. Think of it as a magical filter that selects only the elements you desire.

Challenge: Between 10 and 20?

What if you want to filter elements within a specific range, say, between 10 and 20? You can combine multiple conditions. Here's how you do it:

x[(x > 10) & (x < 20)]


[1] 12 14 16 19

This code filters elements in the vector x that are greater than 10 and less than 20. The & operator represents logical "and." With such powerful filtering capabilities, you can precisely select the data you need for your analysis.

Creating Subsets: The Art of Selection

Subsetting is the art of selecting specific pieces of your data. You can create subsets of data frames using the subset() function by applying conditions to one or more column members.

Let's Experiment with Genes:

Imagine you have a data frame, "datframe," with experimental data. In this dataset, you have genes, genders, and experimental results. You can use the subset() function to create subsets of this data by filtering on individual column values. Let's dive into this experiment:

# Create vectors for genes, gender, and experimental results (result1, result2, ... result6)

# Create a data frame with the data
datframe <- data.frame(genes, gender, result1, result2, result3, result4, result51, result52, result6)

# Create subsets based on different conditions
subframe1 <- subset(datframe, datframe$expt2 > 20)
subframe2 <- subset(datframe, datframe$Gender == "F")
subframe3 <- subset(datframe, (datframe$Gender == "M") & (datframe$expt2 < 30.0))

Here's an overview of the subsets:

  • subframe1 includes rows with expt2 values greater than 20.

  • subframe2 includes rows where Gender is "F."

  • subframe3 includes rows with "Male" gender and expt2 less than 30.0.

Unleashing Vector Operations: Union, Intersection, and More

Vector operations go beyond basic arithmetic. You can perform set operations, cumulative calculations, and find unique elements in vectors. Let's explore these operations:

Union and Intersection:

In R, you can perform set operations like union and intersection on vectors. Given two vectors x and y, you can find their union and intersection like this:

x <- c('A', 'B', 'C', 'D', 'E')
y <- c('D', 'E', 'K', 'L', 'S', 'P')
zu <- union(x, y)
zi <- intersect(x, y)

Difficult Differences:

You can also calculate differences between consecutive elements in a vector. The diff() function in R is your ally in this task:

x <- c(4, 8, 11, 14, 35, 56, 120, 30)
diff_x <- diff(x)
diff_x2 <- diff(x, 2)

Cumulative Magic:

For cumulative calculations, R provides functions like cumsum() and cumprod(). These functions create vectors that store cumulative sums and products:

x <- c(1, 3, 5, 4, 6, 8, 2)
cumulative_sum <- cumsum(x)
cumulative_product <- cumprod(x)

Unique and Duplicated:

You can find unique elements in a vector and locate duplicated ones. R offers unique() and duplicated() functions for these tasks:

x <- c('a', 'b', 'a', 'c', 'e', 'f', 'c', 'g', 'h')
unique_x <- unique(x)
duplicated_x <- duplicated(x)
duplicated_elements <- x[duplicated(x)]
non_duplicated_elements <- x[!duplicated(x)]

Finding the Index:

If you want to find the index of a specific element in a vector, you can use the which() function. Let's say you want to find the index of "ddd" in a vector x:

x <- c("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh")
index_of_ddd <- which(x == "ddd")

With these vector operations, you can perform a wide range of tasks, from basic calculations to more advanced data manipulations.

Merging and Joining Data Frames: Crafting Data Magic

Data is rarely stored in a single table; you often need to combine or merge data from multiple sources. In R, you can merge and join data frames to create a unified dataset. Let's explore the art of joining data.

Vertical and Horizontal Binding:

You can vertically or horizontally bind data frames to create a larger dataset.

vbframe <- rbind(frame1, frame2)
hbframe <- cbind(frame1, frame2)

Vertical binding (rbind) stacks data frames on top of each other, assuming they have the same column structure.

Horizontal binding (cbind) combines data frames side by side, assuming they have the same number of rows.

Merging Data Frames: The Art of Joining

Merging is more powerful and flexible. You can merge data frames based on common columns.

mrgA <- merge(frame1, frame3, by = "product")
mrgB <- merge(frame1, frame3, by = c("index", "product"))

Here, we merge frame1 and frame3 based on the "product" column. You can also specify multiple columns as the joining key, as shown in mrgB.

Merging and joining techniques in R provide powerful tools to combine data from different sources into a single, comprehensive dataset.

In this blog, we've explored the magic of vector operations in R, from basic filtering to handling missing data, creating subsets, and performing advanced operations like set operations, cumulative calculations, and finding unique elements. We've also delved into the world of merging and joining data frames, crafting data magic by combining and merging data from different sources.

With these skills in your data science toolkit, you'll be better equipped to analyze and manipulate your data effectively, unlocking the true potential of your datasets. Start practicing these techniques, and you'll be well on your way to becoming a data alchemist, turning raw data into valuable insights.