【发布时间】:2018-12-05 04:20:04
【问题描述】:
这是来自unordered combination and store the result in a matrix in r 的另一个问题。
现在我有一个如下的数据框
>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
我想提取每行中等于 1 的元素,使用其列名并排列它们,以存储在新的数据框中,例如第一行的 day_1_1:
> 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"
我的问题是我不知道如何在循环中创建那些名为day_1_i(i 匹配plan 中的行号)的新数据框。我试过了
for (i in 1:nrow(plan)) {
paste0("day_1_", i) <- permutations(3, 3, v = names(plan)[which(plan[i,] == 1, arr.ind=T)[, "col"]])
}
但它不起作用。我从Using a loop to create multiple data frames in R 看到了一种使用assign 的可能解决方案,但建议不要使用。非常感谢您的建议!
【问题讨论】:
标签: r