Friday, February 16, 2018

How do you change LongFormat to WideFormat in R programming?

In my previous post you must have seen the difference between WIDE and LONG formats and also learnt how to convert a WIDE formatted data to LONG format.

In this post, you will see the converting of LONG format to WIDE format.

In this case you need to use the spread() function.

Let us start with the LONG formatted data of the previous post.
------------------------------
   Day Segment condition measurement
    1      AM      Jack        2.30
    2      AM      Jack        3.50
    3      PM      Jack        2.50
    4      PM      Jack        3.00
    1      AM      Jane        3.51
    2      AM      Jane        4.20
    3      PM      Jane        3.90
    4      PM      Jane        4.00
    1      AM       Tom        1.70
    2      AM       Tom        3.90
    3      PM       Tom        2.10
    4      PM       Tom        2.50
---------------------------------------
At first, read LONG data using read.table().

LongtoWide1.png

Use this code to convert this to WIDE format:
<- condition="" data_long="" measurement="" p="" spread="">Before you use spread(), you should have the tidyr library as shown here:


<- condition="" data_long="" measurement="" p="" spread="">LongtoWide2.png
<- condition="" data_long="" measurement="" p="" spread="">
<- condition="" data_long="" measurement="" p="" spread="">Note: Both gather() and spread() takes considerable time to process.

No comments: