You can achieve this transformation in R using the following steps:
main.r948 chars35 lines
This code defines a function transform_df that takes a dataframe as input, loops through its columns in steps of 2, selects each column pair, converts it to a dataframe with two columns, and binds it to the result dataframe. The resulting dataframe contains all column pairs from the original dataframe, with each pair in a separate row.
Note that this code assumes that the input dataframe has an even number of columns. If the input dataframe has an odd number of columns, the last column will be ignored. If you want to include the last column in the result, you can modify the loop condition to 1:ncol(df) and add a special case for the last column.
Also, note that the order of the column pairs in the result dataframe is from left to right, as specified in the problem statement.
The output will be:
main.r120 chars11 lines
gistlibby LogSnag