在DataFrame R / Compinatorics中找到可行的组合

发布于 2025-01-19 15:03:30 字数 647 浏览 1 评论 0原文

我有以下挑战: 具有218个观测值(行)和218个变量(COLS)的数据框架。 值是真或错误。 现在,我需要找到至少2行出现(true)的变量(col)组合。

以下是一个示例:

data <- data.frame(matrix(FALSE, nrow = 3, ncol = 5))
colnames(data) = paste("item_", 1:5, sep = "")
rownames(data) = paste("Process_", 1:3, sep = "")
data["Process_1",c("item_1","item_2","item_3")] = TRUE
data["Process_2",c("item_2","item_3")] = TRUE
data["Process_3",c("item_1","item_2","item_3","item_4","item_5")] = TRUE

在示例中,可行组合(或发现的目标)是以下组合:

c1:item1,item2,item3 c2:item2

c2:item3

c3:item3 c3:item1,item2

c4:item2 c4:item1,item1,item3

谢谢你,非常感谢您回答或提示很多:)

欢呼

I have the following challenge:
dataframe with 218 observations (rows) and 218 variables (cols).
The values are either TRUE or FALSE.
Now i need to find combinations of variables (cols) that appear (TRUE) in at least 2 rows.

Here is a little example:

data <- data.frame(matrix(FALSE, nrow = 3, ncol = 5))
colnames(data) = paste("item_", 1:5, sep = "")
rownames(data) = paste("Process_", 1:3, sep = "")
data["Process_1",c("item_1","item_2","item_3")] = TRUE
data["Process_2",c("item_2","item_3")] = TRUE
data["Process_3",c("item_1","item_2","item_3","item_4","item_5")] = TRUE

For the example the feasible combinations (or the goal to find out) are the following combinations:

c1: item1,item2,item3

c2: item2,item3

c3: item1, item2

c4: item1, item3

Thank you very much for an answer or a hint :)

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

但可醉心 2025-01-26 15:03:30
#all items that have TRUE in 2 or more rows
items <- names(which(colSums(data) >= 2))
# all possible combinations of 2 (or more) items
lapply(2:length(items), function(x) combn(items, x)
# [[1]]
#          [,1]     [,2]     [,3]    
# [1,] "item_1" "item_1" "item_2"
# [2,] "item_2" "item_3" "item_3"
# 
# [[2]]
#          [,1]    
# [1,] "item_1"
# [2,] "item_2"
# [3,] "item_3"
#all items that have TRUE in 2 or more rows
items <- names(which(colSums(data) >= 2))
# all possible combinations of 2 (or more) items
lapply(2:length(items), function(x) combn(items, x)
# [[1]]
#          [,1]     [,2]     [,3]    
# [1,] "item_1" "item_1" "item_2"
# [2,] "item_2" "item_3" "item_3"
# 
# [[2]]
#          [,1]    
# [1,] "item_1"
# [2,] "item_2"
# [3,] "item_3"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文