2
I have the following data frame:
dput(exemplo)
structure(list(id = c(1, 2, 3, 4, 5, 6, 7),
    grupo = c("A", "A", "A", "B", "B", "B", NA),
    fc_pre = c(90, 98, 77, 98, 100, 92, 89),
    fc_pos = c(70, 77, 77, 70, 79, 72, 76),
    pa_pre = c(130, 140, 160, 160, 120, 120, 150),
    pa_pos = c(120, 110, 140, 150, 130, 120, 130)),
   .Names = c("id", "grupo", "fc_pre", "fc_pos", "pa_pre", "pa_pos"), 
    class = c("tbl_df", "tbl", "data.frame"),
    row.names = c(NA, -7L))
I put it in long format using the code:
    library(reshape2)
    longo <- melt(exemplo, id=c("id", "grupo"))
My data frame was in the long format. But, R put fc_pre a fc_pos a pa_pre and pa_pos in the same vector.
    longo
    id grupo variable value
 1   1     A   fc_pre    90
 2   2     A   fc_pre    98
 3   3     A   fc_pre    77
 4   4     B   fc_pre    98
 5   5     B   fc_pre   100
 15  1     A   pa_pre   130
 16  2     A   pa_pre   140
 17  3     A   pa_pre   160
 18  4     B   pa_pre   160
 19  5     B   pa_pre   120
I’d like it to be this way:
 # A tibble: 14 x 5
       id grupo tempo     fc     pa
    <dbl> <chr> <chr>  <dbl>  <dbl>
  1     1     A   pre     90    130
  2     2     A   pre     98    140
  3     3     A   pre     77    160
  4     4     B   pre     98    160
  5     5     B   pre    100    120
  6     6     B   pre     92    120
  8     1     A   pos     70    120
  9     2     A   pos     77    110
 10     3     A   pos     77    140
 11     4     B   pos     70    150
 12     5     B   pos     79    130
See that now I have a vector called time, another called Fc and another called pa. Would anyone know how to restructure my data frame to look like this?
Thank you Guilherme Parreira, it worked here. I managed to reproduce the example. I just need to understand some functions. Thank you
– Nelson Carvas Junior