-1

enter image description here

this is my dataset

But, I want to look like this

enter image description here

Phil
  • 7,287
  • 3
  • 36
  • 66
Mdallal
  • 31
  • 4
  • Is there a specific issue? Please see [ask], [help/on-topic]. Also, please do not share information as images unless absolutely necessary. See: https://meta.stackoverflow.com/q/303812, https://meta.stackoverflow.com/q/285551, https://idownvotedbecau.se/imageofcode, https://idownvotedbecau.se/imageofanexception/. – AMC Nov 29 '20 at 02:43
  • Error in UseMethod("pivot_wider") : no applicable method for 'pivot_wider' applied to an object of class "function" – Mdallal Nov 29 '20 at 05:10
  • library("reshape2") library MM<-dcast(setDT(mydata), ID~Indicator , value.var=c('Time1', 'Time2', 'Time3', 'Time4', 'Time5')) head(MM,20) – Mdallal Nov 29 '20 at 05:19

2 Answers2

0

Given the data:

# A tibble: 12 x 8
     ID Indicator   Time1   Time2   Time3  Time4   Time5 Gender
  <dbl> <chr>       <dbl>   <dbl>   <dbl>  <dbl>   <dbl>  <dbl>
1     1 Test1     -1.89    0.0497  0.747   0.274 -1.17        1
2     1 Test2     -0.889  -1.05   -0.610  -0.557 -1.19        1
3     1 Test3      1.27    0.743  -0.0735 -0.501  0.263       1
4     2 Test1     -0.0948 -0.510   1.01   -0.651 -0.258       1
5     2 Test2      0.543  -0.444  -0.956   1.56   0.340       1
6     3 Test1     -0.779   0.159   1.35   -0.599 -0.988       0
7     3 Test2      0.527  -0.567  -1.26   -1.01   0.304       0
8     3 Test3     -0.862   0.506  -1.65    0.687  0.912       0
9     3 Test4     -0.0454 -0.0165  0.0810  1.57  -0.223       0

Replace df with the name of your data frame in the following:

     df %>% 
       pivot_wider(id_cols = c('ID', 'Gender'), 
                   values_from = starts_with('Time'), 
                   names_from = Indicator)

And you will get:

# A tibble: 4 x 22
     ID Gender Time1_Test1 Time1_Test2 Time1_Test3 Time1_Test4 Time2_Test1 Time2_Test2 Time2_Test3 Time2_Test4 Time3_Test1
  <dbl>  <dbl>       <dbl>       <dbl>       <dbl>       <dbl>       <dbl>       <dbl>       <dbl>       <dbl>       <dbl>
1     1      1     -1.89        -0.889       1.27      NA           0.0497      -1.05        0.743     NA            0.747
2     2      1     -0.0948       0.543      NA         NA          -0.510       -0.444      NA         NA            1.01 
3     3      0     -0.779        0.527      -0.862     -0.0454      0.159       -0.567       0.506     -0.0165       1.35 
4     4      0      1.31         0.340      -0.294     NA          -0.827        0.714       0.904     NA           -0.496
# … with 11 more variables: Time3_Test2 <dbl>, Time3_Test3 <dbl>, Time3_Test4 <dbl>, Time4_Test1 <dbl>, Time4_Test2 <dbl>,
#   Time4_Test3 <dbl>, Time4_Test4 <dbl>, Time5_Test1 <dbl>, Time5_Test2 <dbl>, Time5_Test3 <dbl>, Time5_Test4 <dbl>

dput() of the data from this example (not identical to OP's):

structure(list(ID = c(1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4), Indicator = c("Test1", 
"Test2", "Test3", "Test1", "Test2", "Test1", "Test2", "Test3", 
"Test4", "Test1", "Test2", "Test3"), Time1 = c(-1.88857139765869, 
-0.889289318962443, 1.27161954881659, -0.0947604341392057, 0.54317534737885, 
-0.778683702208533, 0.527386676259164, -0.862086001312162, -0.0454187935109571, 
1.30848861542209, 0.340196863228601, -0.294265629654217), Time2 = c(0.0496862703249387, 
-1.04591015073307, 0.742713109597678, -0.510413140920957, -0.4438337253821, 
0.158856144682682, -0.567291639024244, 0.506054624820541, -0.0164843252900173, 
-0.827108251898983, 0.713711557651905, 0.904396726864514), Time3 = c(0.746976724444874, 
-0.609687059979751, -0.0735354326437822, 1.00771841411273, -0.956188806522995, 
1.34724917843572, -1.26090154440256, -1.65026973467026, 0.0810181330966657, 
-0.495601525246375, -0.803680545374548, -0.239653990286531), 
    Time4 = c(0.273685060771852, -0.556830520170204, -0.500971070108688, 
    -0.650984015712442, 1.55571610232803, -0.598877028804694, 
    -1.00967524778919, 0.686785599536373, 1.57231939632508, 1.38059069276428, 
    -1.08177539264527, 0.222354347959881), Time5 = c(-1.17184462221795, 
    -1.19078311888797, 0.263073563588143, -0.258358930207838, 
    0.340370399314113, -0.987912227961785, 0.303503263732513, 
    0.91205315974401, -0.22323508156116, 0.0716037331205316, 
    -1.54890326149943, -0.936290128931749), Gender = c(1, 1, 
    1, 1, 1, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))
Dij
  • 1,318
  • 1
  • 7
  • 13
  • Error in UseMethod("pivot_wider") : no applicable method for 'pivot_wider' applied to an object of class "function" – Mdallal Nov 29 '20 at 05:11
  • In the example above, I had pretended the data were called `df`. `df` is also a function, hence the error you got. Replace `df` with the name of your actual data frame. – Dij Nov 29 '20 at 05:26
0

library("reshape2") library

MM<-dcast(setDT(ww), ID~Indicator , value.var=c('Time1', 'Time2', 'Time3', 'Time4', 'Time5')) head(MM,20)

Mdallal
  • 31
  • 4