If you need to overwrite multiple columns in a data frame with new values, you can use the mutate_at function from the dplyr package. The mutate_at function allows you to apply a transformation function to a specified set of columns. Here's how you can do it:
mutate_at with a List of Columns# Install and load the dplyr package install.packages("dplyr") library(dplyr) # Create a sample data frame df <- data.frame( A = c(1, 2, 3), B = c(4, 5, 6), C = c(7, 8, 9), D = c(10, 11, 12) ) # Define the columns you want to transform columns_to_transform <- c("A", "B", "C") # Apply a transformation function to the specified columns df <- df %>% mutate_at(vars(columns_to_transform), function(x) x * 2) # Display the transformed data frame print(df) across with mutate (recommended for newer versions of dplyr)The across function is more modern and flexible than mutate_at and is recommended for use in newer versions of dplyr.
# Install and load the dplyr package install.packages("dplyr") library(dplyr) # Create a sample data frame df <- data.frame( A = c(1, 2, 3), B = c(4, 5, 6), C = c(7, 8, 9), D = c(10, 11, 12) ) # Apply a transformation function to the specified columns df <- df %>% mutate(across(c(A, B, C), ~ . * 2)) # Display the transformed data frame print(df) dplyr: If not already installed, install the dplyr package and load it.df is created for demonstration purposes.columns_to_transform specifies the columns you want to apply the transformation to.mutate_at:mutate_at(vars(columns_to_transform), function(x) x * 2): vars(columns_to_transform) selects the columns to be transformed. function(x) x * 2 is the transformation function applied to each selected column.mutate with across:mutate(across(c(A, B, C), ~ . * 2)): across(c(A, B, C), ~ . * 2) selects columns A, B, and C and applies the transformation function ~ . * 2 to each of them.The across function is more versatile and is part of the dplyr 1.0.0+ release, making it the preferred choice for transforming multiple columns in newer code.
"How to use mutate_at to overwrite multiple columns in a dataframe in R"
Description: Use mutate_at from the dplyr package to apply a function to multiple columns and overwrite their values.
Code:
library(dplyr) df <- data.frame(a = 1:5, b = 6:10, c = 11:15) df <- df %>% mutate_at(vars(a, b), ~ . * 2) print(df)
Explanation: Multiplies the values in columns a and b by 2, overwriting the existing values.
"Overwrite selected columns using mutate_at with a custom function in R"
Description: Apply a custom function to selected columns using mutate_at to modify their values.
Code:
library(dplyr) df <- data.frame(a = 1:5, b = 6:10, c = 11:15) custom_function <- function(x) { x^2 } df <- df %>% mutate_at(vars(a, b), custom_function) print(df) Explanation: Applies a custom function that squares the values in columns a and b.
"Using mutate_at to apply a function to numeric columns and overwrite them in R"
Description: Apply a function to all numeric columns and overwrite their values using mutate_at.
Code:
library(dplyr) df <- data.frame(a = 1:5, b = 6:10, c = letters[1:5]) df <- df %>% mutate_at(vars(where(is.numeric)), ~ . + 10) print(df)
Explanation: Adds 10 to all numeric columns while leaving non-numeric columns unchanged.
"Overwrite columns with mutate_at based on column names in R"
Description: Use mutate_at to apply a transformation to columns specified by their names.
Code:
library(dplyr) df <- data.frame(a = 1:5, b = 6:10, c = 11:15) df <- df %>% mutate_at(c('a', 'b'), ~ . / 2) print(df) Explanation: Divides the values in columns a and b by 2.
"Apply a function to multiple columns with mutate_at and overwrite in R"
Description: Overwrite values in multiple columns using a function with mutate_at.
Code:
library(dplyr) df <- data.frame(a = 1:5, b = 6:10, c = 11:15) df <- df %>% mutate_at(vars(a, b), function(x) { ifelse(x %% 2 == 0, x, NA) }) print(df) Explanation: Replaces odd numbers with NA in columns a and b.
"Overwrite dataframe columns conditionally using mutate_at in R"
Description: Use mutate_at to conditionally overwrite column values.
Code:
library(dplyr) df <- data.frame(a = 1:5, b = 6:10, c = 11:15) df <- df %>% mutate_at(vars(a, b), ~ ifelse(. > 7, ., 0)) print(df)
Explanation: Sets values in columns a and b to 0 if they are less than or equal to 7.
"How to overwrite dataframe columns with mutate_at using a lambda function in R"
Description: Use a lambda function to overwrite dataframe columns with mutate_at.
Code:
library(dplyr) df <- data.frame(a = 1:5, b = 6:10, c = 11:15) df <- df %>% mutate_at(vars(a, b), ~ . - 1) print(df)
Explanation: Subtracts 1 from the values in columns a and b.
"Using mutate_at to scale columns and overwrite values in R"
Description: Scale values in specific columns using mutate_at and overwrite the original values.
Code:
library(dplyr) df <- data.frame(a = 1:5, b = 6:10, c = 11:15) df <- df %>% mutate_at(vars(a, b), ~ scale(.)[,1]) print(df)
Explanation: Scales columns a and b to have a mean of 0 and a standard deviation of 1.
"Overwrite specific columns in dataframe using mutate_at based on column index in R"
Description: Apply transformations to columns specified by their indices using mutate_at.
Code:
library(dplyr) df <- data.frame(a = 1:5, b = 6:10, c = 11:15) df <- df %>% mutate_at(vars(1, 2), ~ . * 3) print(df)
Explanation: Multiplies values in the first and second columns by 3.
"Overwrite multiple columns with different functions using mutate_at in R"
Description: Apply different functions to different columns using mutate_at.
Code:
library(dplyr) df <- data.frame(a = 1:5, b = 6:10, c = 11:15) df <- df %>% mutate_at(vars(a), ~ . + 5) %>% mutate_at(vars(b), ~ . * 2) print(df)
Explanation: Adds 5 to column a and multiplies values in column b by 2.
azure-databricks azure-pipelines manytomanyfield cjk customization python-module metadata marie android-paging-library nsdictionary