Converting wide format to long format

To convert wide format to long foramt, you can use push!() function.

Example: Dealing with time series data

wide_df = data.frame(
  subj = c("Tom", "Mary", "Jack"),
  t0 = c( 50, 42, 80),
  t1 = c( 48, 42, 75),
  t2 = c( 46, 44, 72),
  t3 = c( 42, 42, 73)
)
code = '
  subj = subj

  time = 0
  bw = t0
  push!()

  time = 1
  bw = t1 
  push!()

  time = 2
  bw = t2
  push!()

  time = 3
  bw = t3
'

Note that push!() is not called at the end of this script. Values at the end of script are always stored into result dataframe implecitly.

library(datasailr)
sail(wide_df , code = code , fullData = F)
##    subj time bw
## 1   Tom    0 50
## 2   Tom    1 48
## 3   Tom    2 46
## 4   Tom    3 42
## 5  Mary    0 42
## 6  Mary    1 42
## 7  Mary    2 44
## 8  Mary    3 42
## 9  Jack    0 80
## 10 Jack    1 75
## 11 Jack    2 72
## 12 Jack    3 73