0

I have some data which has features which are of the same "class" (or type).

x <- tibble(char = c("a", "b", "c", "d"), x1 = c(1:4), x2 = c(11:14))
x
# A tibble: 4 x 3
   char    x1    x2
  <chr> <int> <int>
1     a     1    11
2     b     2    12
3     c     3    13
4     d     4    14

Now, I what I am looking for is a function to collapse x1 and x2 into a single column x.

y <- some_transformation_function(x)
y
# A tibble: 8 x 2
   char     x
  <chr> <int>
1     a     1
2     b     2
3     c     3
4     d     4
5     a    11
6     b    12
7     c    13
8     d    14

I am not looking for a function that modifies the data in anyway (which is what existing questions seem to answer - I haven't found a similar question to what I'm asking).

lohithbb
  • 128
  • 1
  • 11
  • 1
    You're looking for `gather` `x <- tibble(char = c("a", "b", "c", "d"), x1 = c(1:4), x2 = c(11:14))` you can gather the columns by position 2:3, and call them "colname", and then their contents will go in "values". Then we can remove colname. `gather(x, "colname", "values", 2:3) %>% select(-colname)` – Matt W. Nov 20 '17 at 14:34
  • Thank you, this works. I still do not understand the basis of the gather() in tidyr so I would appreciate a little bit of explanation. Specifically, how is the key, "colname", generated (in this case before you drop it, its value are "x" and "y") ? – lohithbb Nov 20 '17 at 14:47
  • 1
    you choose columns - in this cause it's 2:3, so columns 2 through 3 by position, then you name 2 columns, the first column is what the column headers will turn into, the 2nd column is what the values in them will replace. So if you had `char, x1, x2, x3, x4, x5` for columns, you could have gathered 2:6 for all of the x columns, and called it `x_level`, and then `values`. And it would reshape it by taking everything in x1, and making x1 the `x_level`, and then each value the corresponding value, and continue on to x2, x3 etc. – Matt W. Nov 20 '17 at 14:51
  • 1
    Yeah you just pass it a vector of names. `gather(x, "x_level", "values", c("x1", "x2"))` – Matt W. Nov 20 '17 at 15:02
  • Thanks - I experimented with it, and finally got there just as you said it. Thanks for all the help! – lohithbb Nov 20 '17 at 15:03
  • np! good luck man – Matt W. Nov 20 '17 at 15:04

0 Answers0