"Stretching" Tables in R

I have the following data in R:

name = c("person1", "person2", "person3", "person1", "person2", "person3")
var1 = c(1,3,5,6,7,3)
var2 = c(1,1,1,1,1,1)
var3 = rnorm(6,5,6)
var4 = rnorm(6,1,6)
iteration_round = c(1,1,1,2,2,2)

frame = data.frame(name, var1, var2, var3, var4, iteration_round)

    name var1 var2      var3       var4 iteration_round
1 person1    1    1 21.931815  0.4904012               1
2 person2    3    1 -2.261360 -2.3358434               1
3 person3    5    1  5.493085  5.7269291               1
4 person1    6    1 -5.039148 -9.4483417               2
5 person2    7    1 -3.662336  5.8499795               2
6 person3    3    1  5.297230 -2.8756166               2

I am trying to format the data to look like this, so that the columns look like this (the "iteration_round" column will now only have unique values 1,2,3,..) :

person1_var1 person1_var2 person1_var3 person1_var4 person2_var1 person2_var2, person2_var3, person2_var4 person3_var1 person3_var2, person3_var3, person3_var4  iteration_round

I tried to do this with the data.table library

library(data.table)

my_table = dcast(setDT(frame),  var1 + var2 + var3 + var4  ~  iteration_round , value.var = 'name')

But this is not giving the desired result:

 var1 var2      var3       var4       1       2
1:    1    1 21.931815  0.4904012 person1    <NA>
2:    3    1 -2.261360 -2.3358434 person2    <NA>
3:    3    1  5.297230 -2.8756166    <NA> person3
4:    5    1  5.493085  5.7269291 person3    <NA>
5:    6    1 -5.039148 -9.4483417    <NA> person1
6:    7    1 -3.662336  5.8499795    <NA> person2

Can someone please show me how to fix this?

Thanks

library(tidyverse)

frame <- tibble(
  name = c("person1", "person2", "person3", "person1", "person2", "person3"),
  var1 = c(1,3,5,6,7,3),
  var2 = c(1,1,1,1,1,1),
  var3 = rnorm(6,5,6),
  var4 = rnorm(6,1,6),
  iteration_round = c(1,1,1,2,2,2)
)

frame %>% 
  pivot_wider(names_from = name, 
              values_from = c(var1:var4),
              names_glue = "{name}_{.value}") %>% 
  select(sort(tidyselect::peek_vars()))
#> # A tibble: 2 × 13
#>   iteration_round person1_var1 person1_var2 person1_var3 person1_var4
#>             <dbl>        <dbl>        <dbl>        <dbl>        <dbl>
#> 1               1            1            1        5.34         -5.43
#> 2               2            6            1       -0.227         2.97
#> # … with 8 more variables: person2_var1 <dbl>, person2_var2 <dbl>,
#> #   person2_var3 <dbl>, person2_var4 <dbl>, person3_var1 <dbl>,
#> #   person3_var2 <dbl>, person3_var3 <dbl>, person3_var4 <dbl>

Created on 2021-10-11 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.