-2

I currently have data spread out across multiple columns in R. I am looking for a way to put this information into the one column as a vector for each of the individual rows.

Is there a function to do this?

For example, the data looks like this:

DF <- data.frame(id=rep(LETTERS, each=1)[1:26], replicate(26, sample(1001, 26)), Class=sample(c("Yes", "No"), 26, TRUE))

select(DF, cols=c("id", "X1","X2", "X23", "Class"))

How can I merge the columns "X1","X2", "X23" into a vector containing numeric type variables for each of the IDs?

Data Example

r2evans
  • 141,215
  • 6
  • 77
  • 149
user8410799
  • 3
  • 1
  • 3
  • Will this gather the data in a column as a list for each row? – user8410799 Aug 23 '17 at 14:44
  • 4
    Please show a reproducible example of what you have and what you want at the end. At the moment, it sounds like a duplicate of [this question](https://stackoverflow.com/questions/16596515/aggregating-by-unique-identifier-and-concatenating-related-values-into-a-string). – lmo Aug 23 '17 at 14:47
  • Do either of `tidyr::unite` or `tidyr::gather` (two very different functions) do what you are thinking? It's a bit unclear at the moment. – r2evans Aug 23 '17 at 14:57
  • Having sample data is a good start, can you add your expected output? – r2evans Aug 23 '17 at 15:15
  • Im looking to have all the values of different X_ in the one column X but stored as a list. For ID A I would like the first entry of the new X column as c(733, 70, 118) ? – user8410799 Aug 23 '17 at 15:18

1 Answers1

1

Like this?

library(reshape2)
melt(df) %>% dcast(id ~ ., fun.aggregate = list)

Using id, Class as id variables
   id             .
1   A  422, 74, 439
2   B 879, 443, 923
3   C 575, 901, 749
4   D  813, 747, 21
5   E 438, 526, 675
6   F 863, 562, 474
7   G 103, 713, 918
8   H 585, 294, 525
9   I  115, 76, 175
10  J 953, 379, 926
11  K 679, 439, 377
12  L 816, 624, 538
13  M 678, 226, 142
14  N 667, 369, 586
15  O 795, 422, 248
16  P  165, 22, 612
17  Q 294, 476, 746
18  R 968, 368, 290
19  S 238, 481, 980
20  T 921, 482, 741
21  U  550, 15, 296
22  V 121, 358, 625
23  W 213, 313, 242
24  X    92, 77, 58
25  Y 607, 936, 350
26  Z  660, 42, 275

A note though: I do not know your final use case, but this strikes me as something you probably do not want to have. It is often more advisable to stick to tidy data, see e.g. https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html

coffeinjunky
  • 11,254
  • 39
  • 57