This is a further question from unordered combination and store the result in a matrix in r.
Now I have a data frame as below
>head(plan)
bal midway coro cab ljc ot
1 1 1 1 2 2 2
2 1 1 2 1 1 2
3 1 1 2 1 2 2
4 1 1 2 2 1 2
5 1 1 2 2 2 1
6 1 2 1 1 2 2
I want to extract elements in each row which are equal to 1, use its column name and permutate them, to store in a new data frame, say day_1_1
for the first row:
> permutations(3, 3, v = names(plan)[which(plan[1,] == 1, arr.ind=T)[, "col"]])
[,1] [,2] [,3]
[1,] "bal" "coro" "midway"
[2,] "bal" "midway" "coro"
[3,] "coro" "bal" "midway"
[4,] "coro" "midway" "bal"
[5,] "midway" "bal" "coro"
[6,] "midway" "coro" "bal"
My problem is I don't know how to create those new data frame called day_1_i
(i
matches the row number in plan
) in a loop. I've tried
for (i in 1:nrow(plan)) {
paste0("day_1_", i) <- permutations(3, 3, v = names(plan)[which(plan[i,] == 1, arr.ind=T)[, "col"]])
}
But it doesn't work. I've seen one possible solution using assign
from Using a loop to create multiple data frames in R, but it's suggested to not use. Thanks a lot for advise!