How to use across() for Multiple Columns In R Language
The across() function allows you to apply the same transformation to multiple columns simultaneously.
# Create a new sample data frame
data <- data.frame(
id = 1:5,
math_score = c(88, 92, 85, 87, 90),
science_score = c(80, 85, 82, 84, 88)
)
# Load dplyr package
library(dplyr)
# Apply a transformation across multiple columns
data <- data %>%
mutate(across(c(math_score, science_score), ~ . * 1.1))
# Display the updated data frame
print(data)
Output:
id math_score science_score
1 1 96.8 88.0
2 2 101.2 93.5
3 3 93.5 90.2
4 4 95.7 92.4
5 5 99.0 96.8
How to Change value of variable with dplyr
The Dplyr package in R is a powerful tool for data manipulation and transformation. It provides a set of functions that allow you to perform common data manipulation tasks concisely and efficiently. One of these tasks is changing the value of a variable within a data frame. This article will guide you through various methods to change the value of a variable using dplyr.