My database is not in a form I can use

I would like to transform my database, because I don't know how to use it in the current form.
I don't know how to explain what I want, so I tried to illustrate it in excel. X is a number.

I am incredibly happy for help!

The data can be reshaped with the pivot_longer function from the tidyr package. I shortened the column names to save myself some typing.

DF <- data.frame(Subject = 1:5, HR_A = 2:6, HR_B = 12:16, HR_C = 22:26, HR_D = 32:36, HR_E = 42:46)
DF
#>   Subject HR_A HR_B HR_C HR_D HR_E
#> 1       1    2   12   22   32   42
#> 2       2    3   13   23   33   43
#> 3       3    4   14   24   34   44
#> 4       4    5   15   25   35   45
#> 5       5    6   16   26   36   46
library(tidyr)
library(dplyr)

DFlong <- DF %>% pivot_longer(cols = HR_A:HR_E, names_to = "Situation", values_to = "HR")
DFlong
#> # A tibble: 25 × 3
#>    Subject Situation    HR
#>      <int> <chr>     <int>
#>  1       1 HR_A          2
#>  2       1 HR_B         12
#>  3       1 HR_C         22
#>  4       1 HR_D         32
#>  5       1 HR_E         42
#>  6       2 HR_A          3
#>  7       2 HR_B         13
#>  8       2 HR_C         23
#>  9       2 HR_D         33
#> 10       2 HR_E         43
#> # … with 15 more rows
DFlong <- DFlong %>% mutate(Situation = sub("HR_", "", Situation))
DFlong
#> # A tibble: 25 × 3
#>    Subject Situation    HR
#>      <int> <chr>     <int>
#>  1       1 A             2
#>  2       1 B            12
#>  3       1 C            22
#>  4       1 D            32
#>  5       1 E            42
#>  6       2 A             3
#>  7       2 B            13
#>  8       2 C            23
#>  9       2 D            33
#> 10       2 E            43
#> # … with 15 more rows

Created on 2022-04-16 by the reprex package (v0.2.1)

Wow, thank you so much for the effort! Have a nice day!

This topic was automatically closed 7 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.