Luis
Luis

Reputation: 1584

Using tidyverse to divide / split / break a variable into other variables by groups

Let's say I have this kind of dataset

ds <- data.frame(group = c("A","B","C"),
                 value = rnorm(90,10,15))

Now, I want to use dplyr (or purrr + broom) to "divide" the value variable into three columns, like the following image. new dataset

I've tried pivot_wider, group_split, and separate, but with no success.

In the future, I'll use these variables to compute correlation coefficients. I would like to remain in the tidyverse environment. Thank you

Code:

ds <- data.frame(group = c("A","B","C"),
                 value = rnorm(90,10,15))

Upvotes: 0

Views: 441

Answers (2)

henhesu
henhesu

Reputation: 851

Sticking to a tidyverse context as requested, the problem with pivot_wider is that there are duplicate row identifiers. Grouping by group doesn't solve that problem either. Personally I believe @Duck's solution is a viable one, but if you really want to stick to the tidyverse, this comment provides a workaround (and this blog post discusses it in more detail). The idea is to add a unique row identifier with row_number(), then spreading the data:

ds %>% 
  group_by(group) %>% 
  mutate(grouped_id = row_number()) %>% 
  pivot_wider(names_from = group, values_from = value)

You can then remove or replace the grouped_id variable as desired.

Upvotes: 1

Duck
Duck

Reputation: 39623

Maybe this can solve your problem:

df <- unstack(ds,value~group)

Upvotes: 1

Related Questions