r - R数据帧中基于静态函数的逻辑函数

标签 r dplyr boolean tidyverse logical-operators

我有一个非常大的 Excel 电子表格,其中包含许多对观察结果的“检查”(300 多列)。检查包括 boolean 运算符(大于、等于)和一些求和/减法:

df <-data.frame(checkID = c(1,2,3,4), checkpart1 = c(50, 70, 111, 320),
                 checkpart2 = c("+", "==", "*", ">"), checkpart3 = c(18, 17, 6, 3), checkpart4 =  c("==", NA, "-", NA), checkpart5 = c(80, NA,76,NA), checkpart6 = c(NA, NA, "==", NA), checkpart7 = c(NA,NA,590, NA))
  
head(df) ##this is the input
#checkID checkpart1 checkpart2 checkpart3 checkpart4 checkpart5 checkpart6 checkpart7
#1           50          +         18         ==         80       <NA>         NA
#2           70         ==         17       <NA>         NA       <NA>         NA
#3          111          *          6          -         76         ==        590
#4          320          >          3       <NA>         NA       <NA>         NA

INSERT CODE THAT MAKES THE EXCEL FUNCTIONS COME TO LIFE HERE. 
Mind you that some rows have much longer checks than others, so you can't rely on the column names. 

#outcome data frame should look like this, where the checks have been conducted:
View(outputchecks)
#checkID
#1   FALSE      
#2   FALSE
#3   TRUE        
#4   TRUE   
有谁知道 R 中的某些 tidyr/dplyr/其他应用程序可以在数据帧中执行这些“静态函数”?
谢谢!

最佳答案

使用 pmap

df <-data.frame(checkID = c(1,2,3,4), checkpart1 = c(50, 70, 111, 320),
                checkpart2 = c("+", "==", "*", ">"), checkpart3 = c(18, 17, 6, 3), checkpart4 =  c("==", NA, "-", NA), checkpart5 = c(80, NA,76,NA), checkpart6 = c(NA, NA, "==", NA), checkpart7 = c(NA,NA,590, NA))


library(tidyverse)
df %>% mutate(exp = pmap_lgl(df[-1], ~ eval(parse(text = paste(na.omit(c(...)), collapse = '')))))
#>   checkID checkpart1 checkpart2 checkpart3 checkpart4 checkpart5 checkpart6
#> 1       1         50          +         18         ==         80       <NA>
#> 2       2         70         ==         17       <NA>         NA       <NA>
#> 3       3        111          *          6          -         76         ==
#> 4       4        320          >          3       <NA>         NA       <NA>
#>   checkpart7   exp
#> 1         NA FALSE
#> 2         NA FALSE
#> 3        590  TRUE
#> 4         NA  TRUE
创建于 2021-07-04 由 reprex package (v2.0.0)

df <-data.frame(checkID = c(1,2,3,4), checkpart1 = c(50, 70, 111, 320),
                checkpart2 = c("+", "==", "*", ">"), checkpart3 = c(18, 17, 6, 3), checkpart4 =  c("==", NA, "-", NA), checkpart5 = c(80, NA,76,NA), checkpart6 = c(NA, NA, "==", NA), checkpart7 = c(NA,NA,590, NA))


library(tidyverse)
df %>% group_by(checkID) %>%
  mutate(across(everything(), ~ifelse(is.na(.), '', as.character(.)))) %>%
  rowwise() %>%
  mutate(exp = eval(parse(text = paste(c_across(everything()), collapse = ''))))

# A tibble: 4 x 9
# Rowwise:  checkID
  checkID checkpart1 checkpart2 checkpart3 checkpart4 checkpart5 checkpart6 checkpart7 exp  
    <dbl> <chr>      <chr>      <chr>      <chr>      <chr>      <chr>      <chr>      <lgl>
1       1 50         +          18         "=="       "80"       ""         ""         FALSE
2       2 70         ==         17         ""         ""         ""         ""         FALSE
3       3 111        *          6          "-"        "76"       "=="       "590"      TRUE 
4       4 320        >          3          ""         ""         ""         ""         TRUE 

transmute将导致
df %>% group_by(checkID) %>%
  mutate(across(everything(), ~ifelse(is.na(.), '', as.character(.)))) %>%
  rowwise() %>%
  transmute(exp = eval(parse(text = paste(c_across(everything()), collapse = ''))))

# A tibble: 4 x 2
# Rowwise:  checkID
  checkID exp  
    <dbl> <lgl>
1       1 FALSE
2       2 FALSE
3       3 TRUE 
4       4 TRUE 

使用 summarise也将删除组
df %>% group_by(checkID) %>%
  mutate(across(everything(), ~ifelse(is.na(.), '', as.character(.)))) %>%
  rowwise() %>%
  summarise(exp = eval(parse(text = paste(c_across(everything()), collapse = ''))), .groups = 'drop')

# A tibble: 4 x 2
  checkID exp  
    <dbl> <lgl>
1       1 FALSE
2       2 FALSE
3       3 TRUE 
4       4 TRUE 

关于r - R数据帧中基于静态函数的逻辑函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68245151/

相关文章:

r - 使用 zoo/xts 删除 R 中的特定值

r - 编写 tidyeval 函数以重命名 dplyr 中的因子级别

r - 如何将dplyr中的动态列名传递给自定义函数?

r - dplyr 中的向量化列运算

java - 具有输入值的二维 boolean 数组

R data.frame 因子而不是级别

r - plotly:通过下拉选择更新数据

r - 使用多个ID列和值列通过pivot_longer()简化gather()

python - 如何在 df 中保留 dtype ('o' ) 而不是 bool

javascript - 偏置随机 boolean 值的优雅方式