I'd like to create a new dataframe, called 'new_df,' by resampling the original 'df.' I want the new dataframe to have its 'A' column with a mean of 80 and a standard deviation of 30.
set.seed(42)
# Original data frame
d <- data.frame(
A = rnorm(1000, 100, 50),
B = rnorm(1000, 20, 10))
# Function to rescale data
rescale_data <- function(x, new_mean, new_sd) {
old_mean <- mean(x)
old_sd <- sd(x)
(((x - old_mean) / old_sd) * new_sd) + new_mean
}
# Resample data frame with new mean and standard deviation
d_resampled <- data.frame(
A = rescale_data(d$A, new_mean = 80, new_sd = 30),
B = rescale_data(d$B, new_mean = 80, new_sd = 30)
)
# Check the mean and standard deviation of the resampled data
mean(d_resampled$A) # Should be close to 80
#> [1] 80
sd(d_resampled$A) # Should be close to 30
#> [1] 30
mean(d_resampled$B) # Should be close to 80
#> [1] 80
sd(d_resampled$B) # Should be close to 30
#> [1] 30
What I'm looking for is not a new dataset with rescaled columns, but rather a resampled version of the original dataset where at least one column meets certain criteria, such as mean and standard deviation.