Amending a dataset to present values in a different format using pivot_wider

Hi All. I'm still new to R but making progress with the help
of this group. I'm struggling with what I consider to be a problem
that can be solved with pivot_wider, though i've not succeeded yet.
Thanks in advance for any assistance!

I have a dataset as below:

    RaceNum Horse             DrawA row_c
     <int> <chr>              <dbl> <int>
 1   21127 A'Ali            -0.242      1
 2   20260 A'Ali             0.128      2
 3   17889 A'Ali            NA          3
 4   21712 A'Shamardi       -0.519      1
 5   20340 A'Shamardi        0.767      2
 6    5285 A Bit Of A Touch NA          1
 7    4825 A Bit Of A Touch -0.0256     2
 8    4207 A Bit Of A Touch  0.139      3
 9    3397 A Bit Of Ginger   0.704      1
10    3206 A Bit Of Ginger   0.152      2
# ... with 64,290 more rows

My goal is to create an output that gives:

Horse			DrawA_1		DrawA_2		DrawA_3
A'Ali			-0.242		0.128		NA
A'Shamardi		-0.519		0.767		NA
A Bit of a Touch	NA		-0.0256		0.139
etc

Basically for every Horse, I need to output the DrawA value for the 3
instances.
Perhaps pivot_wider is not the optimal way to do this?
I was trying to output what I needed using group_by(Horse) and then
using summarise to display the output in a dataset 4 columns wide.
Hope someone can help!

You can try:

tidyr::pivot_wider(df, names_prefix = "DrawA_", names_from = row_c, values_from = row_c)

Thanks Julien! Much appreciated.

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