Reputation: 21
I hope you are doing well.
This is the dataset I'm using
I want to calculate the change of the BMI
of the users of the App. I got the Id's, the first date of record and the last with the following code
BMI_results <- weight_log %>%
group_by(Id) %>%
summarize(date_min = min(Date), date_max = max(Date))
BMI_results
The next part is to use that data to get the corresponding BMI of that dates and then substract those numbers. This is the part where I'm stuck. Please help.
# The following code is wrong, just for illustration:
BMI_results <- weight_log %>%
group_by(Id) %>%
summarize(date_min = min(Date), date_max = max(Date)) %>%
mutate(
first_BMI = filter(weight_log$BMI,
Id == Id & date_min == Date),
last_BMI = filter(weight_log$BMI,
Id == Id & date_max == Date),
difference = first_BMI-last_BMI
)
I was expecting to get the values of each BMI, the first and last recorded, and then substract them.
Upvotes: 1
Views: 34
Reputation: 206187
Try something like
BMI_results <- weight_log %>%
group_by(Id) %>%
arrange(Date) %>%
summarize(diff=first(BMI)-last(BMI))
Upvotes: 0