1

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

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

2 Answers2

2

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   
Karthik S
  • 11,348
  • 2
  • 11
  • 25
0

What you want to do is turning turning long data to wide data and there are many answers to that. Many prefer the pivot_wider function in the tidyr package.

Package data.table has melt and dcast: https://cran.r-project.org/web/packages/data.table/vignettes/datatable-reshape.html

Or you could use the reshape2 package: https://github.com/hadley/reshape

The R inbuild function reshape is somewhat cumbersome to use and I do not recommend it.

Bernhard
  • 4,272
  • 1
  • 13
  • 23