pivot / dataframe organisation

Hi everyone !

I am trying to optimise a dataframe and I am sure that the adequat function exists, tho I can't find it.

    ...1 Value Name                               ID      
   <dbl> <chr> <chr>                              <chr>   
 1     1 Sandy Soil Type                          AAF_2020
 2     2 50    Height Above Sea Level             AAF_2020
 3     3 703   Long Term Mean Of Precipitation    AAF_2020
 4     4 12.4  Long Term Mean Of Temperature      AAF_2020
 5     5 2     Experimental Design                AAF_2020
 6     6 4     Plot Length                        AAF_2020
 7     7 1     Plot Width                         AAF_2020
 8     8 2     Number of Plots                    AAF_2020
 9     9 4     Number Of Rows Per Plot            AAF_2020
10    10 0.35  Distance Between Rows Within Plots AAF_2020

I want the 'Name' column as columns heads, with the values filling te columns, and a column with the IDs.

With the function 'pivot' I can only get a giant datatable with the same number of raws (and most of the datatable presents NA values):

A tibble: 237 × 23
    ...1 ID     Soil …¹ Heigh…² Long …³ Long …⁴ Exper…⁵ Plot …⁶ Plot …⁷ Numbe…⁸
   <dbl> <chr>  <chr>   <chr>   <chr>   <chr>   <chr>   <chr>   <chr>   <chr>  
 1     1 AAF_2… Sandy   NA      NA      NA      NA      NA      NA      NA     
 2     2 AAF_2… NA      50      NA      NA      NA      NA      NA      NA     
 3     3 AAF_2… NA      NA      703     NA      NA      NA      NA      NA     
 4     4 AAF_2… NA      NA      NA      12.4    NA      NA      NA      NA     
 5     5 AAF_2… NA      NA      NA      NA      2       NA      NA      NA     
 6     6 AAF_2… NA      NA      NA      NA      NA      4       NA      NA     
 7     7 AAF_2… NA      NA      NA      NA      NA      NA      1       NA     
 8     8 AAF_2… NA      NA      NA      NA      NA      NA      NA      2      
 9     9 AAF_2… NA      NA      NA      NA      NA      NA      NA      NA     
10    10 AAF_2… NA      NA      NA      NA      NA      NA      NA      NA    

And that's not good ^^

Thank you for your help !!

I think you are looking for pivot_wider in the tidyr package. Something like this:

longdata %>%
   select(ID, Value, Name) %>% # need to remove that counter column as we don't need it
   pivot_wider(id_cols=ID, names_from=Name, values_from=Value)

Hi, unfortunately pivot.wider does not work for this table, as I want to reduce the number or raws without increasing the number of culums.

You cant have both...

That's the question, how? ^^
I tried some function like group_by and unite() but I'm stuck

pivot_wider(somedata,
            id_cols="ID",
            names_from = "Name",
            values_from = "Value")

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