Perlnika Perlnika - 7 days ago 6
R Question

Combine arbitrary number of columns into new table in R

I am reading a table which has 3n columns. Now I need to create new table which will have n+1 columns.

New format will be first column, followed by second divided by third column, followed by fifth divided by sixth column..

1.column 2./3. 5./6. 8./9. 11./12.


So that this:

a b c d e f g h i


will become

a b/c e/f h/i

Answer
cbind(df[, 1], df[, seq(from = 2, to = ncol(df), by = 3)] /
               df[, seq(from = 3, to = ncol(df), by = 3)])