How can we find the mean of one column with respect to another?

In iris dataset, there are five columns, i.e., Sepal.Length, Sepal.Width, Petal.Length, Petal.Width and Species. We will calculate the mean of Sepal-Length across different species of iris flower using the mean() function from the mosaic package.

mean(iris$Sepal.Length~iris$Species)
In R, you can use the aggregate function to find the mean of one column with respect to another. Here’s an example assuming you have a data frame called df:
# Sample data frame
df <- data.frame(Group = c(“A”, “A”, “B”, “B”, “A”, “B”),
Value = c(10, 15, 20, 25, 30, 35))

# Using aggregate to find the mean of ‘Value’ with respect to ‘Group’
result <- aggregate(Value ~ Group, data = df, FUN = mean)

# Print the result
print(result)
This will output:

css

Group Value
1 A 18.33333
2 B 26.66667
In this example, the mean of the ‘Value’ column is calculated for each unique value in the ‘Group’ column. Adjust the column names and data frame according to your specific case.