r - 根据行条件过滤数据框

标签 r if-statement rows data-manipulation

我想到了以下示例来说明我的问题。

假设有5个球:

  • 红色
  • 蓝色
  • 绿色
  • 黄色
  • 橙色

enter image description here

通常有 5 个! = 这些球有 120 种组织方式(n!)。我可以在下面列举所有这些组合:

library(combinat)
library(dplyr)

my_list = c("Red", "Blue", "Green", "Yellow", "Orange")

d = permn(my_list)

all_combinations  = as.data.frame(matrix(unlist(d), ncol = 120)) %>%
  setNames(paste0("col", 1:120))


all_combinations[,1:5]

    col1   col2   col3   col4   col5
1    Red    Red    Red    Red Orange
2   Blue   Blue   Blue Orange    Red
3  Green  Green Orange   Blue   Blue
4 Yellow Orange  Green  Green  Green
5 Orange Yellow Yellow Yellow Yellow

我的问题:

假设我想按以下条件过滤此列表:

  • “红”球可以在第一或第二位置(从左到右)
  • “蓝”球和“绿”球之间必须至少有两个位置
  • “黄”球不能在最后一个位置

然后我尝试根据这 3 个条件过滤上述数据:

# attempt to write first condition
    cond_1 <- all_combinations[which(all_combinations[1,]== "Red" || all_combinations[2,] == "Red"), ]

#not sure how to write the second condition
    
 # attempt to write the third condition   
    cond_3 <- data_frame_version[which(data_frame_version[5,] !== "Yellow" ), ]

# if everything worked, an "anti join" style statement could be written to remove "cond_1, cond_2, cond_3" from the original data?

但这根本不起作用 - 第一个和第三个条件返回一个数据框,所有列仅包含 4 行。

有人可以告诉我如何使用上述 3 个过滤器正确过滤“all_combinations”吗?

注意:

下面的代码可以转置原始数据:

 library(data.table)

    tpose = transpose(all_combinations)

    df = tpose
    
#group every 5 rows by the same id to identify unique combinations

    bloc_len <- 5
    
    df$bloc <- 
        rep(seq(1, 1 + nrow(df) %/% bloc_len), each = bloc_len, length.out = nrow(df))
    
   
 head(df)

      V1     V2     V3     V4     V5 bloc
1    Red   Blue  Green Yellow Orange    1
2    Red   Blue  Green Orange Yellow    1
3    Red   Blue Orange  Green Yellow    1
4    Red Orange   Blue  Green Yellow    1
5 Orange    Red   Blue  Green Yellow    1
6 Orange    Red   Blue Yellow  Green    2

最佳答案

你可以这样做:

library(tidyverse)
tpose %>%
  mutate(blue_delete = case_when(V1 == "Blue" & V2 == "Green" ~ TRUE,
                                 V1 == "Blue" & V3 == "Green" ~ TRUE,
                                 V2 == "Blue" & V3 == "Green" ~ TRUE,
                                 V3 == "Blue" & V4 == "Green" ~ TRUE,
                                 V4 == "Blue" & V5 == "Green" ~ TRUE,
                                 TRUE ~ FALSE)) %>%
  filter(V3 != "Red" & V4 != "Red" & V5 != "Red",
         V5 != "Yellow",
         blue_delete == FALSE) %>%
  select(-blue_delete)

关于r - 根据行条件过滤数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70574980/

相关文章:

php - 变量中的 If 逻辑(谓词表达式)

java - 无法编译if语句

javascript - 从之前的 if 语句中获取值

r - 将数据框折叠成单行并根据行 R 创建新列

python - 从 Pandas DataFrame 中选择与另一个 DataFrame 中的列值完全相同的行

r - 一种将 "survfit"对象的某些元素提取到数据框中的简洁方法

r - ggplot - 多个箱线图

r - 计算中位数

vb.net - asp.net vb 网站循环遍历数据库行

r - 并行计算 : Loading packages in each thread only once