0

I am working with the R programming language.

Suppose I have the following data:

my_data <- data.frame(

"id" = c("1", "1", "1", "1", "2", "2", "2", "2" ),
"name" = c("john", "jason", "jack", "jim", "john", "jason", "jack", "jim" ),
"points" = c("150", "165", "183", "191", "151", "166", "184", "192"),
"gender" = c("male", "male", "male", "male", "male", "male", "male", "male"),
"country" = c("usa", "usa", "usa", "usa", "usa", "usa", "usa", "usa")
)

#view original data format
 my_data

  id  name points gender country
1  1  john    150   male     usa
2  1 jason    165   male     usa
3  1  jack    183   male     usa
4  1   jim    191   male     usa
5  2  john    151   male     usa
6  2 jason    166   male     usa
7  2  jack    184   male     usa
8  2   jim    192   male     usa

Let's assume that for the above data: "gender" and "country" will always have the same values. Furthermore, these 4 names will always appear together - each time they appear together, the "id" for all of them is the same number. The only number that can change is the number of "points" that they have from iteration to iteration (i.e., their "id").

Question: Is there any way that the above data set can be automatically transformed into the below data set?

my_data_1 <- data.frame(

"id" = c("1", "2"),
"john_points" = c("150", "151"),
"jason_points" = c("165", "166"),
"jack_points" = c("183", "184"),
"jim_points" = c("191", "192"),
"gender" = c("male", "male"),
"country" = c("usa", "usa")
)

#view desired data format

  my_data_1
  id john_points jason_points jack_points jim_points gender country
1  1         150          165         183        191   male     usa
2  2         151          166         184        192   male     usa

Is there any way that this data can be "collapsed", and the variable names can be automatically renamed as "name_points" for each individual (e.g. john_points, jack_points, etc.)?

Currently, I am trying to do this manually in Excel - but is there a way to automatically "collapse" this data in R?

Thanks

stats_noob
  • 3,127
  • 2
  • 8
  • 27
  • 1
    Your operation is a simple "reshape to wide format". Search for this and you will find at least three approaches. – Roland Jul 04 '21 at 06:21

0 Answers0