0

I have a large data set which is currently in the following format:

Label  X   Y   pond
1     172 107   A
1     655 130   A
1     761 169   A

but it needs to be formatted in the following way:

Label X1   X2  X3   Y1   Y2   Y3   pond
1     172  655 761  107  130  169   A

Many thanks for your kind attention.

Axeman
  • 27,115
  • 6
  • 69
  • 82
  • `library(tidyverse); df %>% gather(key, val, X:Y) %>% group_by(key) %>% mutate(col = paste0(key, 1:n())) %>% ungroup() %>% select(-key) %>% spread(col, val)` – Maurits Evers Dec 04 '18 at 00:36
  • or `library(data.table); dcast(setDT(df)[, n := 1:.N], Label + pond ~ n, value.var = c("X", "Y"))` – Maurits Evers Dec 04 '18 at 00:40
  • Hi, Many thanks. But I am getting the following error message "Error: cannot allocate vector of size 6.1 Gb", while trying formatting in tidyverse. How can this be solved? Kindest regards. – Mehedi Hasan Dec 04 '18 at 01:34
  • Hi Mehedi Hasan. That error is pretty self-explanatory; the simplest answer would be to increase your RAM. An alternative would be to rethink your computational data-analytical approach. Usually keeping data in a tidy (long) format is desirable. Is there a particular reason why you need data to be in such an unwieldy (wide) format? – Maurits Evers Dec 04 '18 at 03:17

0 Answers0