all.
Very new to R and working with data more generally. Have also never written anything in Stack Overflow or similar, so please forgive any dodgy formatting or terminology.
I have a dataframe that needs converting from long to wide format ready for a paired T test. The data frame is:
> head(df)
# A tibble: 6 x 13
Assessor Product Ap Ar F T
<dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1 1 MC 10 10 10 10
2 1 MV 10 10 10 10
3 2 MC 6 7 8 8
4 2 MV 7 5 4 8
5 3 MV 9 9 10 9
6 3 MC 6 8 7 6
# ... with 7 more variables:
# C1 <dbl>,
# JCo <dbl>,
# JSt1 <dbl>,
# JSt2 <dbl>,
# JSw <dbl>,
# JCr <dbl>,
# OA <dbl>
I need to put it in the following wide format but for all variables, where each column after Product is converted into two columns, one for each product (MC & MV):
Assessor Ap_MC Ap_MV Ar_MC Ar_MV
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1 10 10 10 10
2 2 6 7 7 5
3 3 9 6 9 8
The headings don't strictly need to be these, but need to differentiate that they are grouped by the different products as well as the original column.
I've seen various approaches to converting between long and wide format on here, but I can't make any work. This was an attempt I made using pivot_wider() (error message included):
df <- pivot_wider(df, id_cols = df[1], names_from = df[2], values_from = df[3:13])
Error: Must subset columns with a valid subscript vector.
x Subscript has the wrong type `tbl_df<Product:character>`.
i It must be numeric or character.
Run `rlang::last_error()` to see where the error occurred.
I have no idea what's causing this error, let alone if my attempt was anywhere close to giving me the output I want.
Thank you in advance!