1

I am trying to create a new dataframe by pulling the count in a few of my columns and repeating that column variable the number of times in the count and it's corresponding values.

Here is an example of what I want to do.

dfex=data.frame(group=c(1,2,3),white=c(5,2,1),black=c(1,3,2),num=c(1,5,10))
head(dfex)
  group white black num
1     1     5     1   1
2     2     2     3   5
3     3     1     2  10

What I want to get is a data frame with three columns, bg, color (either black or white), and then conc. Basically something like this

   group color num
1      1 white   1
2      1 white   1
3      1 white   1
4      1 white   1
5      1 white   1
6      1 black   1
7      2 white   5
8      2 white   5
9      2 black   5
10     2 black   5
11     2 black   5
12     3 white  10
13     3 black  10
14     3 black  10

I'm not sure if I explained it well with words, but I hope the example is descriptive enough to explain what I hope to do.

johnnyg
  • 129
  • 8

3 Answers3

1

Here is a tidyverse - reshape2 solution:

library(reshape2)
library(tidyverse)
melt(dfex, id = c("group", "num")) %>% uncount(value) %>% arrange(group) %>%
        select(group, color = variable, num)
# output
   group color num
1      1 white   1
2      1 white   1
3      1 white   1
4      1 white   1
5      1 white   1
6      1 black   1
7      2 white   5
8      2 white   5
9      2 black   5
10     2 black   5
11     2 black   5
12     3 white  10
13     3 black  10
14     3 black  10
nghauran
  • 6,648
  • 2
  • 20
  • 29
0

Here is one possible solution:

library(tidyverse)
library(splitstackshape)

dfex %>%
  pivot_longer(cols = c(white, black), names_to = "color") %>%
  expandRows("value")
Ben
  • 28,684
  • 5
  • 23
  • 45
0

You could do

library(tidyr)
library(dplyr)

gather(dfex, color, reps, -group, - num) %>% 
  rowwise() %>% 
  mutate(new = toString(rep(num, reps))) %>% 
  separate_rows(new, sep = ", ") %>% 
  select(group, color, num = new)

With uncount as proposed by @nghauran it is much shorter:

gather(dfex, color, reps, -group, - num) %>% 
  uncount(reps) %>% 
  select(group, num, color)
kath
  • 7,624
  • 17
  • 32