1

I hope you are doing well.

This is the dataset I'm using

Data set

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.

Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
  • 1
    Please provdie a [minimal reproducible example](https://stackoverflow.com/q/5963269/10068985), instead of a screenshot. – Darren Tsai Mar 01 '23 at 04:28
  • 1
    Please [do not post code or data in images](https://meta.stackoverflow.com/q/285551/2372064). Share a reproducible format like `dout()` so we can copy/paste into R for testing. – MrFlick Mar 01 '23 at 05:06
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 01 '23 at 09:48

1 Answers1

0

Try something like

BMI_results <- weight_log %>% 
    group_by(Id) %>% 
    arrange(Date) %>%
    summarize(diff=first(BMI)-last(BMI)) 
MrFlick
  • 195,160
  • 17
  • 277
  • 295