comparing pairs in r
# Step 1: Create a data frame with pairs to compare
data <- data.frame(
Pair = c("A-B", "C-D", "E-F", "G-H"),
Value1 = c(10, 15, 20, 25),
Value2 = c(12, 14, 18, 27)
)
# Step 2: Create a new column to store the comparison result
data$Comparison <- NA
# Step 3: Use a loop to compare pairs and update the 'Comparison' column
for (i in 1:nrow(data)) {
if (data$Value1[i] > data$Value2[i]) {
data$Comparison[i] <- "Value1 is greater"
} else if (data$Value1[i] < data$Value2[i]) {
data$Comparison[i] <- "Value2 is greater"
} else {
data$Comparison[i] <- "Values are equal"
}
}
# Step 4: Display the resulting data frame
print(data)
In this example, we create a data frame with pairs of values to compare (Step 1). We then add a new column named 'Comparison' to store the result of the comparisons (Step 2). Using a loop (Step 3), we compare the values in columns 'Value1' and 'Value2' for each pair and update the 'Comparison' column accordingly. Finally, we display the resulting data frame (Step 4).