1

The data set is lay out in long format and doesn't have missing value, which has 4 columns-the 1st column is "id", the 2nd column is "col", the 3rd column is binary variable "vol", and the 4th column is time. Now, I want to convert the value of "rec" in the 2nd column "col" into "rec1,rec2,rec3,..." by id.

For example, for the id from id=1 and id=6, the expected data set should look like as follows

id   col  vol time
1    rec1  1   1
1    rec2  1   2
1    rec3  0   3
2    rec1  1   1
2    rec2  1   2
2    rec3  1   3
3    rec1  0   1
3    rec2  0   2
3    rec3  0   3
4    rec1  1   1
4    rec2  0   2
4    rec3  0   3
5    rec1  1   1
5    rec2  0   2
6    rec1  1   1
6    rec2  1   2
6    rec3  0   3
6    rec4  0   4

The original data set is structured as follows,

structure(list(id = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 
5, 6, 6, 6, 6), col = c("rec", "rec", "rec", "rec", "rec", "rec", 
"rec", "rec", "rec", "rec", "rec", "rec", "rec", "rec", "rec", 
"rec", "rec", "rec"), vol = c(1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 
0, 1, 0, 1, 1, 0, 0), time = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 
2, 3, 1, 2, 1, 2, 3, 4)), row.names = c(NA, 18L), class = "data.frame")->df
fan lin
  • 33
  • 8

2 Answers2

2

Use paste

df$col <- with(df, paste0(col, time))
akrun
  • 874,273
  • 37
  • 540
  • 662
2

We could use str_c from stringr package in a tidyverse workflow:

str_c is equivalent to paste() with the difference that with str_c() the default is no separator.

library(dplyr)
library(stringr)

mutate(df, col = str_c(col, time))

   id  col vol time
1   1 rec1   1    1
2   1 rec2   1    2
3   1 rec3   0    3
4   2 rec1   1    1
5   2 rec2   1    2
6   2 rec3   1    3
7   3 rec1   0    1
8   3 rec2   0    2
9   3 rec3   0    3
10  4 rec1   1    1
11  4 rec2   0    2
12  4 rec3   0    3
13  5 rec1   1    1
14  5 rec2   0    2
15  6 rec1   1    1
16  6 rec2   1    2
17  6 rec3   0    3
18  6 rec4   0    4
TarJae
  • 72,363
  • 6
  • 19
  • 66