Wide and Long Format

There two kind of format of storing data in a tables. We call it (1) wide and (2) long.

Wide format has a column for each variable. While long only has a column that has information of the name of the variable.

Here is the example of wide format

#   ozone   wind  temp
# 1 23.62 11.623 65.55
# 2 29.44 10.267 79.10
# 3 59.12  8.942 83.90
# 4 59.96  8.794 83.97

And below is the long format

#    variable  value
# 1     ozone 23.615
# 2     ozone 29.444
# 3     ozone 59.115
# 4     ozone 59.962
# 5      wind 11.623
# 6      wind 10.267
# 7      wind  8.942
# 8      wind  8.794
# 9      temp 65.548
# 10     temp 79.100
# 11     temp 83.903
# 12     temp 83.968

People find it most easier to record data in wide format. However, many functions in R was designed for long format.

Then how to transform wide into long and vice-versa?

There are two functions in reshape2 library that will do the job.

reshape2 is based around two key functions: melt and cast:

melt takes wide-format data and melts it into long-format data.

dcast takes long-format data and casts it into wide-format data.

Have fun !

Some Useful Link:

(1) Converting Data between Wide and Long
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s