0

I have a large table, which is structured as follows:

 Date        Abbreviation  Value
1 2019-01-01         NR_1     1
2 2020-01-01         NR_1     2
3 2019-01-01         NR_2     3
4 2020-01-01         NR_2     4
5 2019-01-01        NR_12     5
6 2020-01-01        NR_12     6

How can i reconstruct this table to this:

  Date        NR_1  NR_2  NR_12
1 2019-01-01    1    3     5
2 2020-01-01    2    4     6

Thanks a lot!!

Manuel K
  • 67
  • 7

1 Answers1

0

Code:

library('data.table')
dcast(setDT(df), formula = Date ~ Abbreviation)

#          Date NR_1 NR_12 NR_2
# 1: 2019-01-01    1     5    3
# 2: 2020-01-01    2     6    4 

Data:

df <- read.table(text = 'Date        Abbreviation  Value
1 2019-01-01         NR_1     1
      2 2020-01-01         NR_1     2
      3 2019-01-01         NR_2     3
      4 2020-01-01         NR_2     4
      5 2019-01-01        NR_12     5
      6 2020-01-01        NR_12     6', header = TRUE, stringsAsFactors = FALSE)
Sathish
  • 12,453
  • 3
  • 41
  • 59