-1

How can I get from the data.frame below in R to the other data.frame. I am new to dplyr/tidyr, so do not know exactly what functions to use, but I guess it can be done using these packages.

NAME    GROUP  X1  X2
A        G1    1    2
A        G2    1    3
A        G3    4    3
B        G1    3    3 
B        G2    2    3
B        G3    5    4
C        G1    4    3
C        G2    4    1 
C        G3    4    3




NAME  X1_G1  X2_G1   X1_G2  X2_G2  X1_G3  X2_G3
A      1       2        1      3     4       3
B      4       3        3      3     2       3   
C      4       3        4      1     4       3

Thank you for your help!

1 Answers1

1
library(tidyr) #v1.0.0
pivot_wider(df, names_from = GROUP, values_from = c(X1, X2))

# A tibble: 3 x 7
  NAME  X1_G1 X1_G2 X1_G3 X2_G1 X2_G2 X2_G3
  <chr> <int> <int> <int> <int> <int> <int>
1 A         1     1     4     2     3     3
2 B         3     2     5     3     3     4
3 C         4     4     4     3     1     3

PS1: Downvote why?

I know this is a dup, but this library is out for a month and this consider as a new way to solve this problem.

PS2: Dear Moderator, you should clarify before you delete someone's comment.

A. Suliman
  • 12,923
  • 5
  • 24
  • 37