reshaping data using reshape() to wide format instead of dcast or pivot functions

54 Views Asked by At

I am having some problems understanding reshape function and would like to specifically use it as it uses base r.

Consider the following code

month <- c("jan", "feb", "mar", "apr")
hours <- c(40, 40, 35, 37)
days <- c(31, 28, 31, 30)

dat <- data.frame(month, hours, days)

In the above example, month, hours and days are the columns but I would like to transform this so that "hours" and "days" are rows within a column and 4 months in the "month" column are their own columns instead of rows/observations.

Any help would be appreciated. I've tried looking through other examples but can't seem to get my head around it.

3

There are 3 best solutions below

1
C. Murtaugh On BEST ANSWER

I think what you're doing is rotating, aka transposing, the data frame, rather than reshaping it, and you can get what you want with just t(dat) or, to be fancier,

dat.t <- as.data.frame(t(dat)[2:3,])
names(dat.t) <- t(dat)[1,]
print(dat.t)

Which gives you:

      jan feb mar apr
hours  40  40  35  37
days   31  28  31  30
0
dandrews On

I don't use this function much but there are a number of tutorials on it around the web. Here is one that helped me come up with this answer. https://www.datasciencemadesimple.com/reshape-in-r-from-wide-to-long-from-long-to-wide/

Perhaps this is the output you are looking for?

reshape(data = dat,
        idvar = 'month',
        varying = c('hours','days'),
        v.names = 'value',
        times = c('hours','days'),
        direction = 'long')

Although you stated you wanted to use reshape, here is a tidyverse solution (not sure why you would want to avoid it?)

library(tidyverse)
dat %>% 
  pivot_longer(cols = !month,
               names_to = 's',
               values_to = 'y')
0
Andre Wildberg On

Reshaping makes sense if data is in either long or wide format

Bringing the data into long format

dd <- cbind(stack(dat[,-1]), month = dat[,1])

dd
  values   ind month
1     40 hours   jan
2     40 hours   feb
3     35 hours   mar
4     37 hours   apr
5     31  days   jan
6     28  days   feb
7     31  days   mar
8     30  days   apr

then reshaping to wide

reshape(dd, timevar="month", idvar="ind", direction="wide")
    ind values.jan values.feb values.mar values.apr
1 hours         40         40         35         37
5  days         31         28         31         30