# Sample original dataframe set.seed(123) # for reproducibility df <- data.frame( Month = sample(1:12, 1000, replace = TRUE), Month_Day = sample(1:31, 1000, replace = TRUE), Dollars = runif(1000, min = 0, max = 100) ) head(df) # Convert Month to abbreviated month names df$Month <- month.abb[df$Month] # Summarize the data by Month, Month_Day, and Dollar amount summarized_df <- aggregate(Dollars ~ Month + Month_Day, data = df, FUN = sum) # Create an empty matrix to store the results result_matrix <- matrix(0, nrow = 31, ncol = 13, dimnames = list(NULL, c("Day", month.abb))) # Fill in the day column result_matrix[, 1] <- 1:31 # Fill in the dollar amounts for (i in 1:nrow(summarized_df)) { month_idx <- match(summarized_df[i, "Month"], month.abb) day_idx <- as.integer(summarized_df[i, "Month_Day"]) result_matrix[day_idx, month_idx + 1] <- summarized_df[i, "Dollars"] } # Convert the matrix to a dataframe wide_df <- as.data.frame(result_matrix) # Print the resulting dataframe print(wide_df)