October 2019
Beginner to intermediate
674 pages
16h 43m
English
Here, we demonstrate the use of the melt() function in R. It produces long-format data in which the rows are unique variable-value combinations:
>sample4=head(flights.sample,4)[c('year','month','dep_delay','arr_delay')]
> sample4
year month dep_delayarr_delay
155501 2013 3 2 5
2410 2013 1 0 4
64158 2013 11 -7 -27
221447 2013 5 -5 -12
>melt(sample4,id=c('year','month'))
year month variable value
1 2013 3 dep_delay 2
2 2013 1 dep_delay 0
3 2013 11 dep_delay -7
4 2013 5 dep_delay -5
5 2013 3 arr_delay 5
6 2013 1 arr_delay 4
7 2013 11 arr_delay -27
8 2013 5 arr_delay -12
>
Read now
Unlock full access