逐行操作以查看是否有任何列在任何其他列表中 [英] Row-wise operation to see if any columns are in any other list

查看:37
本文介绍了逐行操作以查看是否有任何列在任何其他列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 tibble tib 如下:

I have a tibble tib as follows:

  A     B     C     D    
  <chr> <chr> <chr> <chr>
1 X123  X456  K234  V333 
2 X456  Z000  L888  B323 
3 X789  ZZZZ  D345  O999 
4 M111  M111  M111  M111 
.
.
.
(5000 rows)

我还有另一个向量如下:

I also have another vector as follows:

> vec <- c("X123","X456")
> vec
[1] "X123" "X456"

我正在寻找一种搜索方法,并在 TRUEFALSE 取决于 tib 中列的任何值是否包含 vec 中的值.我的目标输出如下:

I am looking for a way to search for, and add a logical column (with 5000 rows, in the e.g.) to the right of the tibble that is either TRUE or FALSE depending on whether any of the values of the columns in tib contain a value in vec. My goal output is the following:

  A     B     C     D      lgl
<chr> <chr> <chr> <chr>  <lgl>
1 X123  X456  K234  V333   TRUE
2 X456  Z000  L888  B323   TRUE
3 X789  ZZZZ  D345  O999   FALSE
4 M111  M111  M111  M111   FALSE

我有以下几点:

> tib %>% 
+   pmap_lgl(~any(..1 %in% vec))
[1]  TRUE  TRUE FALSE FALSE

这得到了我正在寻求的结果,但我对 语法有点困惑.

This gets the results that I am seeking, but I'm a bit confused about the syntax.

为什么上面的工作(即使用..1),而不是必须使用..1..2..3..4?我的理解是 pmap 基于输入行生成一个向量,所以我假设上面的 ..1 表示向量 c("X123","X456","K234","V333") 行#1,c("X456","Z000","L888","B323") 行#2等

Why does the above work (i.e. using ..1), instead of having to use ..1, ..2, ..3, and ..4? My understanding is that pmap generates a vector based on the inputs rowwise, so I assume that ..1 in the above means the vector c("X123","X456","K234","V333") for row #1, c("X456","Z000","L888","B323") for row #2, etc.

最后,我有两个问题:

  1. 如何将这个新的逻辑向量附加到上述 tib 中?我没有任何运气:
  1. How do I append this new logical vector to the above tib? I haven't had any luck with:

tib %>% mutate(lgl = pmap_lgl(~any(..1 %in% vec)))

mutate_impl(.data, dots) 中的错误:评估错误:缺少参数.f",没有默认值.

  1. 如果我要观察以访问每一行中的每一列(例如,pmap 中第一行的X123"),我将如何在 purrr 的语法中执行此操作?
  1. If I were to watch to access each column within each row (e.g. "X123" for the first row in pmap), how would I do that within the syntax of purrr?

推荐答案

您可以使用 add_columnpmap_lgl 以及辅助函数来获得 tidyverse 类似于@YOLO 的基本 apply 解决方案的单行代码.

You can use add_column and pmap_lgl along with a helper function to get a tidyverse one-liner similar to the base apply solution from @YOLO.

library(tidyverse)

df <- tibble(A = c('X123', 'X456','X789', 'M111'),
             B = c('X456', 'Z000', 'ZZZZ', 'M111'),
             C = c('K234', 'L888', 'D345', 'M111'),
             D = c('V333', 'B323', '0999', 'M111'))


vec <- c('V333', '0999')

check <- function(...) {

  any(c(...) %in% vec)

}

add_column(df, row_check = pmap_lgl(df, check))

# A tibble: 4 x 5
  A     B     C     D     row_check
  <chr> <chr> <chr> <chr> <lgl>    
1 X123  X456  K234  V333  TRUE     
2 X456  Z000  L888  B323  FALSE    
3 X789  ZZZZ  D345  0999  TRUE     
4 M111  M111  M111  M111  FALSE    

在函数中使用 ... 的警告是它将对提供的 tibble 或数据帧的所有列进行操作.如果您有其他列,则需要指定函数参数或限制传递给 pmap_lgl

The caveat of using ... in the function is that it will operate over ALL columns of the provided tibble or data frame. If you have additional columns you'll need to either specify the function arguments or limit the data passed to the pmap_lgl

这篇关于逐行操作以查看是否有任何列在任何其他列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆