Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in R Programming by (7.3k points)
edited by

Take this sample variable

df <- data.frame(month=rep(1:3,2),

                 student=rep(c("Amy", "Bob"), each=3),

                 A=c(9, 7, 6, 8, 6, 9),

                 B=c(6, 7, 8, 5, 6, 7))

I can use spread from tidyr to change this to wide format.

> df[, -4] %>% spread(student, A)

  month Amy Bob

1     1   9   8

2     2   7   6

3     3   6   9

But how can I spread two values e.g. both A and B, such that the output is something like

  month Amy.A Bob.A Amy.B Bob.B

1     1     9     8     6     5

2     2     7     6     7     6

3     3     6     9     8     7

1 Answer

0 votes
by
edited by

To get the desired output, you can use the following code

library(tidyr)

df %>% 

  gather(variable, value, -(month:student)) %>%

  unite(temp, student, variable) %>%

  spread(temp, value)

Output:

  month Amy_A Amy_B Bob_A Bob_B

1     1     9     6     8     5

2     2     7     7     6     6

3     3     6     8     9     7

You can also use the data.table package as follows:

library(data.table)

dcast(setDT(df), month ~ student, value.var = c("A", "B"))

   month A_Amy A_Bob B_Amy B_Bob

1:     1     9     8     6     5

2:     2     7     6     7     6

3:     3     6     9     8     7

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...