Hi,
Welcome to the RStudio community!
What you like to do is not that difficult when you get to know all the functions from the Tidyverse, but it can be a bit of a learning curve in the beginning, so bear with me here.
Let me first show me a possible solution:
library(tidyverse)
#Generate data
set.seed(1)
myData = tibble(
MRN = 1:5,
AGE = sample(18:65, 5, replace = T),
Temperature0hs = runif(5, 36, 40),
Temperature24hs = runif(5, 36, 40),
Temperature48hs = runif(5, 36, 40)
)
myData
#> # A tibble: 5 x 5
#> MRN AGE Temperature0hs Temperature24hs Temperature48hs
#> <int> <int> <dbl> <dbl> <dbl>
#> 1 1 21 38.6 38.7 40.0
#> 2 2 56 38.5 37.5 37.5
#> 3 3 18 36.2 39.1 39.1
#> 4 4 51 36.8 38.0 39.7
#> 5 5 40 36.7 38.9 36.8
#Make the format long
myData = myData %>%
pivot_longer(-c(MRN, AGE), names_to = "time",
values_to = "temp")
myData
#> # A tibble: 15 x 4
#> MRN AGE time temp
#> <int> <int> <chr> <dbl>
#> 1 1 21 Temperature0hs 38.6
#> 2 1 21 Temperature24hs 38.7
#> 3 1 21 Temperature48hs 40.0
#> 4 2 56 Temperature0hs 38.5
#> 5 2 56 Temperature24hs 37.5
#> 6 2 56 Temperature48hs 37.5
#> 7 3 18 Temperature0hs 36.2
#> 8 3 18 Temperature24hs 39.1
#> 9 3 18 Temperature48hs 39.1
#> 10 4 51 Temperature0hs 36.8
#> 11 4 51 Temperature24hs 38.0
#> 12 4 51 Temperature48hs 39.7
#> 13 5 40 Temperature0hs 36.7
#> 14 5 40 Temperature24hs 38.9
#> 15 5 40 Temperature48hs 36.8
#Do analyses
myData %>%
group_by(MRN) %>%
summarise(
minTemp = min(temp),
maxTemp = max(temp),
meanTemp = mean(temp),
rangeTemp = maxTemp - minTemp
)
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 5 x 5
#> MRN minTemp maxTemp meanTemp rangeTemp
#> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 1 38.6 40.0 39.1 1.32
#> 2 2 37.5 38.5 37.9 0.996
#> 3 3 36.2 39.1 38.1 2.86
#> 4 4 36.8 39.7 38.2 2.91
#> 5 5 36.7 38.9 37.5 2.16
Created on 2021-02-03 by the reprex package (v0.3.0)
So I started by recreating a slimmed down version of your dataset, only using 3 temperature moments and 2 other variables.
The key function to make this data easier to work with, it changing the format from wide to long. This is done using the pivot_longer() function and telling it that all columns apart from MRN and AGE need to be changed into a long format. You can see the before and after in the example.
Once you have the data in this long format, it's much easier to work with the other dplyr functions to get the analyses you like. I provided a few examples.
Please read the documentation on the various functions if you like to learn more about them, and let us know if you get stuck again!
Hope this helps,
PJ