[datatable-help] Pivot part of a data table

Stefano Grioni sgrioni at chicagobooth.edu
Sun Nov 8 20:41:40 CET 2015


Hello,

I am new to R and trying to understand how data manipulation works. I have
a CSV file which allows me to build a data table that looks like the
following (simplified example):

>db
   names ages grade_spring_quarter grade_summer_quarter
1:  John   24                    A                   A+
2: Steve   16                   B+                    B
3: Peter   12                    C                   B+

Now, my goal is to denormalize the data table in order to transform the
last 2 columns in rows, therefore obtaining the following:

> db2
   names ages quarter grades
1:  John   24  spring      A
2: Steve   16  spring     B+
3: Peter   12  spring      C
4:  John   24  summer     A+
5: Steve   16  summer      B
6: Peter   12  summer     B+

I think that I would be able to do it using for loops, but I doubt that's
the most efficient way. I have started looking into the Join functionality
of data table but so far didn't manage to build db2 from db that way.

Do you see how I could proceed?

Many thanks

Stefano
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20151108/bec74103/attachment.html>


More information about the datatable-help mailing list