Here's an attempt to solve your problem. Since you don't describe your problem, I can't visualise it and hence I'm answering based on vague ideas without understanding and leaving huge scope of improvement. May be it's very obvious for people familiar with accumarray
, but unfortunately my knowledge is extremely limited.
input_dataset <- 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 = c("s_1", "s_2", "s_3", "s_4"))
temp_fun <- function(t1, t2)
{
temp_input_dataset <- input_dataset[startsWith(x = names(x = input_dataset),
prefix = t2)]
required_subs <- cbind(rep(x = 1:nrow(temp_input_dataset),
times = ncol(x = temp_input_dataset)),
stack(x = temp_input_dataset)$values)
required_vals <- rep(x = 1,
times = nrow(x = required_subs))
required_sz <- c(nrow(x = temp_input_dataset), 10)
temp_output_dataset <- as.data.frame(x = pracma::accumarray(subs = required_subs,
val = required_vals,
sz = required_sz))
names(x = temp_output_dataset) <- paste(t2, (1:10),
sep = ".")
cbind(t1, temp_output_dataset)
}
Reduce(f = temp_fun,
x = c("A", "B", "C"), # in general, supply the vector of variable names here
init = data.frame(subject = c("s_1", "s_2", "s_3", "s_4")))
#> subject A.1 A.2 A.3 A.4 A.5 A.6 A.7 A.8 A.9 A.10 B.1 B.2 B.3 B.4 B.5 B.6
#> 1 s_1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> 2 s_2 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0
#> 3 s_3 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0
#> 4 s_4 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1
#> B.7 B.8 B.9 B.10 C.1 C.2 C.3 C.4 C.5 C.6 C.7 C.8 C.9 C.10
#> 1 0 0 2 0 0 0 1 1 0 0 0 0 0 0
#> 2 0 0 0 2 0 0 0 0 1 1 0 0 0 0
#> 3 0 1 1 0 0 0 0 0 0 0 1 1 0 0
#> 4 0 0 0 0 0 0 0 0 0 0 0 0 0 2
Created on 2019-07-02 by the reprex package (v0.3.0)
Note
Please do not tag in such a extent. You tagged me 3 times from 3 threads on a same question, within a window of around 2 hours!! At least, give me the opportunity to read your post before tagging one more time. I'm sorry if it offends you, but it's not possible for me (and, probably for most of the other people on this community) to be online always. Please familiarise yourself with this thread: