1

currently I have a long dataframe that looks like this:

      PS_NAME             PS_DATE          PARAMETER              NUMBER_VALUE 
      7310    2021-07-07 00:00:00          Total Protein              280.4438                    
      7311    2021-07-09 00:00:00          Total Protein              253.0149                    
      7312    2021-07-14 00:00:00          Total Protein              298.8616                    

I want to change the shape of my dataframe so that each PARAMETER (Total Protein, in this case) is its own column, and the NUMBER_VALUE should populate that column. Something like this:

      PS_NAME             PS_DATE    Total Protein 

   7310                      ""            280.4438 
   7311                      ""            253.0149
   7312                      ""            298.8616

The values under Total Protein should include the values under NUMBER_VALUE

How do I change the shape of my data so that it looks like that?

1 Answers1

1

We can use

library(tidyr)
pivot_wider(df1, names_from = PARAMETER, values_from = NUMBER_VALUE)

-ouptut

# A tibble: 3 x 3
  PS_NAME PS_DATE             `Total Protein`
    <int> <chr>                         <dbl>
1    7310 2021-07-07 00:00:00            280.
2    7311 2021-07-09 00:00:00            253.
3    7312 2021-07-14 00:00:00            299.

data

df <- structure(list(PS_NAME = 7310:7312, PS_DATE = c("2021-07-07 00:00:00", 
"2021-07-09 00:00:00", "2021-07-14 00:00:00"), PARAMETER = c("Total Protein", 
"Total Protein", "Total Protein"), NUMBER_VALUE = c(280.4438, 
253.0149, 298.8616)), class = "data.frame", row.names = c(NA, 
-3L))
akrun
  • 874,273
  • 37
  • 540
  • 662