1

I have been informed that the reshape package has not been actively maintained for a long long while; also, i have noticed that the number coming out from the cast() function is not accurate in my case, so now I am looking into the spread() function in tidyr package. I have gone through a lot of examples and I still don't get how to use it. Can anyone please help explain that in my case?

My data is consisted of multiple variables, of which Grade, Date, and Revenue are the 3 variables I want to reshape as another data frame.

Originally, I was trying to do

cast(mydata, Grade ~ Date, value= "Revenue", sum)

to see how much does every grade earn in total.

The result comes out in a desired format as followed, but the numbers are completely incorrect...

> head(mydata)
                     Grade    001.2020 1. Period 2020 002.2020 2. Period 2020 003.2020 3. Period 2020 004.2020 4. Period 2020
1 5160     Brne                              0                    3773                       0                       0
2 5305     Vleipner                     871751                  872116                  790616                 1086088
3 5315     Digor                        282535                  232953                  368555                  218094
4 5329     Yverker 21                   327740                  308353                  246029                  354357
5 5337     Hanadis 8 S                  167158                  185662                  202147                  135868
6 5339     NADIS 4                      496648                  403006                  693280                  657806
> 

As suggested, the result of dput(head(mydata)) is attached below,

> dput(head(mydata))
structure(list(Quality = structure(1:6, .Label = c("rne", 
"eipner", "gor", "verker 21", 
"nadis 8 S", "xtra Supe", 
"dis 10", "die", "almax", 
"dar 1", "Supre", 
"Mic", "Vik", 
"TIG", "var", "ormvar", 
"olmax", "imax", "imax SR"), class = "factor"), `001.2020 1. Period 2020` = c(0, 
871751, 282535, 327740, 167158, 496648), `002.2020 2. Period 2020` = c(3773, 
872116, 232953, 308353, 185662, 403006), `003.2020 3. Period 2020` = c(0, 
790616, 368555, 246029, 202147, 693280), `004.2020 4. Period 2020` = c(0, 
1086088, 218094, 354357, 135868, 657806)), idvars = "Grade", rdimnames = list(
    structure(list(Grade = structure(1:47, .Label = c("rne", 
"eipner", "gor", "verker 21", "nadis 8 S", "xtra Supe", 
"dis 10", "die", "almax", 
"dar 1", "Supre", 
"Mic", "Vik", 
"TIG", "var", "ormvar", 
"olmax", "imax", "imax SR"), class = "factor")), row.names = c("rne", 
"eipner", "gor", "verker 21", 
"nadis 8 S", "xtra Supe", 
"dis 10", "die", "almax", 
"dar 1", "Supre", 
"Mic", "Vik", 
"TIG", "var", "ormvar", 
"olmax", "imax", "imax SR"), class = "data.frame"), structure(list(
        Period.year = structure(1:4, .Label = c("001.2020 1. Period 2020", 
        "002.2020 2. Period 2020", "003.2020 3. Period 2020", 
        "004.2020 4. Period 2020"), class = "factor")), row.names = c("001.2020 1. Period 2020", 
    "002.2020 2. Period 2020", "003.2020 3. Period 2020", "004.2020 4. Period 2020"
    ), class = "data.frame")), row.names = c(NA, 6L), class = c("cast_df", 
"data.frame"))
> 

Are the numbers supposed to be cleansed before cast()? If so, how so?

If not, then is spread() meant to do the same? If so, how to make the magic happen?

mmkuchi
  • 11
  • 3
  • Please add a reproducible example along with expected output. – Ronak Shah Aug 19 '19 at 07:59
  • @RonakShah Please see the updated content, i have attached the desired format. – mmkuchi Aug 19 '19 at 08:14
  • please use `dput(head(mydata))` to provide example data. it makes things much easier. – JBGruber Aug 19 '19 at 08:21
  • It sounds like your data was already in long format, with no need to `cast` or `spread`. Perhaps try `library(dplyr); mydata %>% group_by(Grade) %>% summarise(Revenue = sum(Revenue))` – Brian Aug 19 '19 at 23:51
  • @Brian I thought `cast` is to transfer a long format into a wide one; didn't know that `cast` is used for a long format... – mmkuchi Aug 20 '19 at 03:30
  • @mmkuchi can you post `head(your_original_data)`? It's not clear what format it's in before you're trying to convert from long to wide. – Brian Aug 20 '19 at 11:58

0 Answers0