Help with finding average


So this is what part of the data I'm using looks like (not mine) and I'm new to R so I'm not quite sure what I'm doing. I'm trying to find the average heart rate (thalach) for each age but i want the age intervals to be 20,40,60,80. Even without the intervals is fine.Is there a simple way to do this? Any help would be greatly appreciated!

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

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!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.