0

Say I wanted to extract every 4th element from mtcars, starting from column 2. I could do:

cars_subdf <- mtcars[seq(2, ncol(mtcars), 4)]

How could I do the reverse of this? e.g. insert cars_df col 1 as mtcars col 2, then column 2 of cars_df four columns along and so on? I know there's interleave and similar, but they only directly 'zippered' e.g. :

1a 1b 2a 2b

Thanks.

TW93
  • 139
  • 1
  • 8
  • Ehrm... try `mtcars[seq(2, ncol(mtcars), 4)] <- cars_subdf`? Or am I not getting what you want to do? – LAP Apr 20 '17 at 09:39
  • If you want to interleave rows, you could add an ID column with odd numbers to the first df (1, 3, 5, etc), and then an ID column with even numbers to the second df (2, 4, 6 etc), and then rbind the two data frames and sort by ID. If you want to interleave columns just [change the column order](http://stackoverflow.com/questions/5620885/how-does-one-reorder-columns-in-a-data-frame) – Kristoffer Winther Balling Apr 20 '17 at 10:09
  • Thanks both. Leo, that worked perfectly. Don't know why I didn't just try it, total mind blank... – TW93 Apr 20 '17 at 10:14

0 Answers0