Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reshape but expand the data in R

Tags:

r

I have the below dataset:

my.data <- read.table(text = '
                  ID  tmc_code  wDay    time_category   TTTR
                  1  121-04711  weekday Afternoon   1.1
                  2  121-04711  weekend Evening     1.3
                  3  121-04711  weekday Morning 1.1
                  4  121-04712  weekend Afternoon   1.101626016
                  5  121-04712  weekday Evening 1.281124498
                  6  121-04712  weekday Morning 1.080645161
                  ', header = TRUE, stringsAsFactors = FALSE, na.strings = 'NA')
my.data

and I would like to have a wide format result like this:

#result
  #          tmc_code    wDay    TTTR_afternnon TTTR_Evening  TTTR_Morning
  #          121-04711  weekday         1.1         1.3           NA
  #          121-04711  weekend         NA          NA            1.1
  #          121-04712  weekday         NA       1.281124498    1.080645161
  #          121-04712  weekend    1.101626016      NA            NA

We can see hat not only reshape function should be used, but actually this process will turn 6 data into 9 data.

The reshape function below does not work for this situation:

w.my.data <- reshape(my.data, idvar = "tmc_code", timevar = "time_category", direction = "wide")

I wonder anyone has better ideas? Much appreciated!

like image 391
Zheng Avatar asked Oct 11 '18 20:10

Zheng


People also ask

What is reshaping of data in R explain with example?

Data Reshaping in R is something like arranged rows and columns in your own way to use it as per your requirements, mostly data is taken as a data frame format in R to do data processing using functions like 'rbind()', 'cbind()', etc. In this process, you reshape or re-organize the data into rows and columns.

How do I make long data wider in R?

The easiest way to reshape data between these formats is to use the following two functions from the tidyr package in R: pivot_longer(): Reshapes a data frame from wide to long format. pivot_wider(): Reshapes a data frame from long to wide format.


1 Answers

You can use the reshape2 package:

> reshape2::dcast(my.data, tmc_code + wDay ~ paste("TTTR", time_category, sep="_"))

Using TTTR as value column: use value.var to override.
   tmc_code    wDay TTTR_Afternoon TTTR_Evening TTTR_Morning
1 121-04711 weekday       1.100000           NA     1.100000
2 121-04711 weekend             NA     1.300000           NA
3 121-04712 weekday             NA     1.281124     1.080645
4 121-04712 weekend       1.101626           NA           NA

Oh, and apparently it works with reshape as well, which also gives a helpful warning about variation in the ID that is being ignored here:

> reshape(my.data, idvar = c("tmc_code", "wDay"), timevar = "time_category", v.names = "TTTR", direction = "wide")

   ID  tmc_code    wDay TTTR.Afternoon TTTR.Evening TTTR.Morning
1:  1 121-04711 weekday       1.100000           NA     1.100000
2:  2 121-04711 weekend             NA     1.300000           NA
3:  4 121-04712 weekend       1.101626           NA           NA
4:  5 121-04712 weekday             NA     1.281124     1.080645
Warning message:
In reshapeWide(data, idvar = idvar, timevar = timevar, varying = varying,  :
  some constant variables (ID) are really varying
like image 115
Frank Avatar answered Oct 10 '22 13:10

Frank