0

Hi guys I have a list that looks like that :

Sample        Value
10            152365
10            236548
10            232547
10            145987
22            98564
22            98745
22            236547

And I would like to make it like this

10           22          
152365     98564
236548     98745 
232547     236547
145987

I have tried pivot_wider, but, since I have over 100'000 values it give me the mistake that some identical values are found and thus cannot work, wheras the spread just simply freeze... Can you help me?

Thanks Lore

Sotos
  • 51,121
  • 6
  • 32
  • 66
  • `split(data$Value, data$Sample)` is what you want? – Ric Dec 06 '22 at 13:49
  • You can't have a data frame in R with different column lengths. You can get a `list` that is approximately in the format you want by doing `split(df$Value, df$Sample)` – Allan Cameron Dec 06 '22 at 13:50

1 Answers1

0

Assuming the blank under 22 can be NA, this works:

library(dplyr)
library(tidyr) # pivot_longer
quux %>%
  group_by(Sample) %>%
  mutate(rn = row_number()) %>%
  pivot_wider(rn, names_from = "Sample", values_from = "Value") %>%
  select(-rn)
# # A tibble: 4 x 2
#     `10`   `22`
#    <int>  <int>
# 1 152365  98564
# 2 236548  98745
# 3 232547 236547
# 4 145987     NA

Data

quux <- structure(list(Sample = c(10L, 10L, 10L, 10L, 22L, 22L, 22L), Value = c(152365L, 236548L, 232547L, 145987L, 98564L, 98745L, 236547L)), class = "data.frame", row.names = c(NA, -7L))
r2evans
  • 141,215
  • 6
  • 77
  • 149