0

I have a table like described below:

in_key in_id ro_id data
42 Text 1 1
42 Text 2 1
43 Text 3 0
43 Text 4 0
44 Text 5 0
44 Text 6 0

I need to transform this table in format:

ro_1 ro_2 ro_3 in_key in_id
Value Value Value 42 Text

Where values of ro_1, ro_2, etc are taken from corresponding values in data column and indexes in column names such as _1, _2, are taken from values in ro_id column. The in_key value should correspond same as table 1.

How to approach to this in R? In SPSS this transformation can be done easily but I would like to see an R solution for this.

Kinds

user438383
  • 5,716
  • 8
  • 28
  • 43
user1997567
  • 439
  • 4
  • 19
  • 1
    `pivot_wider(dat, c(in_key, in_id), names_from = "ro_id", values_from = "data", names_prefix = "ro_")` – r2evans Sep 21 '22 at 12:22
  • 1
    `out <- reshape2::dcast(in_key + in_id ~ ro_id, data = dat, value.var = "data"); names(out)[-(1:2)] <- paste0("ro_", names(out)[-(1:2)]); out` – r2evans Sep 21 '22 at 12:23
  • 1
    Perfect @r2evans thank you. both solutions work, but I prefer first since second generated an error and needed me to remove labels from SPSS table – user1997567 Sep 21 '22 at 12:55

0 Answers0