####################################################################
#Function name: generate_random_data
#Use: Create a random generate data using a for loop.
#Variables required and predate: n_groups=4, n_obs_min=25, n_obs_max=100, mean_min=12, mean_max=40, sd_min=2, sd_max=5
#How it works: Itiate with an empty list to store independent df for each group. Second creacte for loop to generate random parameter for each group base on n_groups.
#Finally combine all dataset in one unique df.
#------------------------------------
generate_random_data <- function(n_groups=4, n_obs_min=25, n_obs_max=100, mean_min=12, mean_max=40, sd_min=2, sd_max=5) {
group_data <- list()
for (i in 1:n_groups) {
n_obs <- sample(n_obs_min:n_obs_max, 1)
group_mean <- runif(1, mean_min, mean_max)
group_sd <- runif(1, sd_min, sd_max)
group_data[[i]] <- data.frame(Group = rep(paste0("Group_", i), n_obs),
Value = rnorm(n_obs, mean = group_mean, sd = group_sd))
}
all_data <- do.call(rbind, group_data)
return(all_data)
}
##################################################
#Function Name: calculate_group_stats
#owner:DPR
#Create summary statistic for a data frame of groups and values. (2 colums)
#The two columns will need to be named Value and Group.
#The function input will only need to be the data frame.
#The output is a dataframe with the summay stats for each group.
#-------------------------------------------------------------------------
calculate_group_stats <- function(data) {
group_stats <- aggregate(Value ~ Group, data = data, FUN = function(x) c(mean = mean(x), median = median(x), sd = sd(x)))
return(group_stats)
}
#---------------------------------------------------------------------------