Hi @jess88,
It would definitely be helpful if you could provide some sample data next time. But since I think I understand what you're trying to do, I went ahead and made a little sample data frame.
Try something like this:
library(dplyr)
# Define some minimal data
df <- data.frame(age = c(63, 37, 41, 56, 57, 57, 56, 44, 52, 57), thalach = c(150, 187, 172, 178, 163, 148, 153, 173, 162, 174)) # I just copied the numbers from the data you showed above
# Group the ages into bins, with cut points at 20, 40, 60, and 80
df <- df %>%
mutate(ageGroup = cut(age, breaks = c(20, 40, 60, 80)))
# Use the group_by() and summarize() functions to get mean heart rate by age group
heartRate <- df %>%
group_by(ageGroup) %>%
summarize(meanHeartRate = mean(thalach))
heartRate #view results
Note that if you have any missing values (NA's) in the thalach column, you'd probably want to add the na.rm = T argument to the mean() function, like this:
heartRate <- df %>%
group_by(ageGroup) %>%
summarize(meanHeartRate = mean(thalach, na.rm = T))
I hope that helps!