0

I have a table like this:

   Anno Mese     `No quotations`
      <int> <chr>                  <int>
    1  2018 Feb                    169
    2  2018 Jan                    147
    3  2019 Feb                    102
    4  2019 Jan                    222

I'd like to transform data like this

              Feb-2018   Jan-2018
No quotation    169         147

and so on. Basically I need time on columns.

I tried melt and reshape with no success.

I can use dcast

dcast(report1, . ~ Anno+Mese)

and that's the output

 . 2018_febbraio 2018_gennaio 2019_febbraio 2019_gennaio
1 .           169          147           102          222

while I'd prefer to have

                  2018_febbraio 2018_gennaio 2019_febbraio 2019_gennaio
No_quotation            169          147           102          222

And also I could have more than one numeri values ( besides No quotations column)

software R. Thanks

Marco Fumagalli
  • 2,307
  • 3
  • 23
  • 41
  • This is ultimately multi-part: (1) figure out how to combine `Anno` and `Mese` into one column (perhaps with `paste`); (2) look into `tidyr::spread` or `data.table::dcast`. (I always have difficult with `reshape` and `reshape2::*`, to be honest, so I strongly encourage you to try the other two I just mentioned.) – r2evans Feb 12 '19 at 16:30

1 Answers1

0

sample data

library(data.table)
dt<-fread("    2018 Feb                    169
                2018 Jan                    147
                2019 Feb                    102
                2019 Jan                    222", header = FALSE)
dt
# V1  V2  V3
# 1: 2018 Feb 169
# 2: 2018 Jan 147
# 3: 2019 Feb 102
# 4: 2019 Jan 222

code

dcast(dt, . ~ paste0(V2, "-", V1), value.var = "V3")
#    . Feb-2018 Feb-2019 Jan-2018 Jan-2019
# 1: .      169      102      147      222
Wimpel
  • 26,031
  • 1
  • 20
  • 37