aggregate
# Assuming you have a data frame named 'df' with columns 'group', 'value1', and 'value2'
# Step 1: Define the formula for aggregation
formula <- value1 ~ group
# Step 2: Use the aggregate function to perform aggregation
aggregated_data <- aggregate(formula, data = df, FUN = sum)
# Step 3: Display the aggregated data
print(aggregated_data)
In this code:
- Replace 'df' with the name of your data frame.
- 'group' is the variable by which you want to aggregate.
- 'value1' is the variable you want to sum within each group.
- The formula value1 ~ group
specifies the aggregation rule.
- The aggregate
function is applied to the data frame using the specified formula and aggregation function (sum
in this case). The result is stored in the 'aggregated_data' variable.
- The aggregated data is then printed.