Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to sort the dataset based on the same certain column values in R

My dataset looks like below

Parameter Date Value
X 1 12
Y 1 14
Z 1 15
X 2 10
Y 2 09
Z 2 22
X 3 08
Y 3 07
Z 3 20

as you can see, we have similar values in column "Parameter" and "Date", I want to convert the rows to column to re-order the data set based on the same values of "Parameter" and "Date".

my expected result is like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Date X Y Z
1 12 14 15
2 10 09 22
3 08 07 20

>Solution :

You are looking for pivoting your data:

library(dplyr)
library(tidyr)

df %>% pivot_wider(Date, names_from = Parameter, values_from = Value)
# A tibble: 3 × 4
   Date X     Y     Z    
  <dbl> <chr> <chr> <chr>
1     1 12    14    15   
2     2 10    09    22   
3     3 08    07    20   
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading