I obtained this code thanks to @Yarnabrina involving accumarray function in order to convert the following input dataset

```
input<-data.frame(
A = c(1L, 3L, 4L, 6L),
A.1 = c(2L, 3L, 7L, 7L),
B = c(9L, 10L, 8L, 5L),
B.1 = c(9L, 10L, 9L, 6L),
C = c(3L, 5L, 7L, 10L),
C.1 = c(4L, 6L, 8L, 10L),
subject = as.factor(c("s_1", "s_2", "s_3", "s_4"))
)
```

into another dataframe represented by a new matrix containing zeros and, eventually, 1 or 2 values in correspondence of the values for the variables A, B and C of the input dataset, as follows:

```
output<-(
A.1 = c(1L, 0L, 0L, 0L),
A.2 = c(1L, 0L, 0L, 0L),
A.3 = c(0L, 2L, 0L, 0L),
A.4 = c(0L, 0L, 1L, 0L),
A.5 = c(0L, 0L, 0L, 0L),
A.6 = c(0L, 0L, 0L, 1L),
A.7 = c(0L, 0L, 1L, 1L),
A.8 = c(0L, 0L, 0L, 0L),
A.9 = c(0L, 0L, 0L, 0L),
A.10 = c(0L, 0L, 0L, 0L),
B.1 = c(0L, 0L, 0L, 0L),
B.2 = c(0L, 0L, 0L, 0L),
B.3 = c(0L, 0L, 0L, 0L),
B.4 = c(0L, 0L, 0L, 0L),
B.5 = c(0L, 0L, 0L, 1L),
B.6 = c(0L, 0L, 0L, 1L),
B.7 = c(0L, 0L, 0L, 0L),
B.8 = c(0L, 0L, 1L, 0L),
B.9 = c(2L, 0L, 1L, 0L),
B.10 = c(0L, 2L, 0L, 0L),
C.1 = c(0L, 0L, 0L, 0L),
C.2 = c(0L, 0L, 0L, 0L),
C.3 = c(1L, 0L, 0L, 0L),
C.4 = c(1L, 0L, 0L, 0L),
C.5 = c(0L, 1L, 0L, 0L),
C.6 = c(0L, 1L, 0L, 0L),
C.7 = c(0L, 0L, 1L, 0L),
C.8 = c(0L, 0L, 1L, 0L),
C.9 = c(0L, 0L, 0L, 0L),
C.10 = c(0L, 0L, 0L, 2L),
subject = as.factor(c("s_1", "s_2", "s_3", "s_4"))
)
```

I wrote the following code but I'd like to make it as a dynamic loop since the input table could contain a different number of variables (still with 2 values per variable).

```
required_subsA <- cbind(rep(x = 1:nrow(input),
times = 2),c(input$A,input$A.1))
required_vals <- rep(x = 1,
times = nrow(x = required_subs))
required_sz <- c(nrow(x = input), 10)
A <- cbind(paste("s", 1:4, sep = "_"),
as.data.frame(x = pracma::accumarray(subs = required_subsA,
val = required_vals,
sz = required_sz)))
names(x = A) <- c("subject",paste("A", 1:10, sep = "."))
required_subsB <- cbind(rep(x = 1:nrow(input),
times = 2),c(input$B,input$B.1))
B <- cbind(as.data.frame(x = pracma::accumarray(subs = required_subsB,
val = required_vals,
sz = required_sz)))
names(x = B) <- c(paste("B", 1:10, sep = "."))
required_subsC <- cbind(rep(x = 1:nrow(input),
times = 2),c(input$C,input$C.1))
C <- cbind(as.data.frame(x = pracma::accumarray(subs = required_subsC,
val = required_vals,
sz = required_sz)))
names(x = C) <- c(paste("C", 1:10, sep = "."))
data_final<-cbind(A,B,C)
```