R:从多列中创建新的基于列的值列表

R:从多列中创建新的基于列的值列表,r,mutate,R,Mutate,我想根据多个列中的列表中的任何值创建一个新列(T/F)。在本例中,我使用mtcars作为示例,在两列中搜索两个值,但我的实际挑战是在多个列中搜索多个值 我使用下面包含的filter\u at()成功地进行了筛选,但我无法将该逻辑应用于变种: # there are 7 cars with 6 cyl mtcars %>% filter(cyl == 6) # there are 2 cars with 19.2 mpg, one with 6 cyl, one with 8 mtca

我想根据多个列中的列表中的任何值创建一个新列(T/F)。在本例中,我使用mtcars作为示例,在两列中搜索两个值,但我的实际挑战是在多个列中搜索多个值

我使用下面包含的
filter\u at()
成功地进行了筛选,但我无法将该逻辑应用于变种:

# there are 7 cars with 6 cyl
mtcars %>%
  filter(cyl == 6)

# there are 2 cars with 19.2 mpg, one with 6 cyl, one with 8
mtcars %>% 
  filter(mpg == 19.2)

# there are 8 rows with either.
# these are the rows I want as TRUE
mtcars %>% 
  filter(mpg == 19.2 | cyl == 6)

# set the cols to look at
mtcars_cols <- mtcars %>% 
  select(matches('^(mp|cy)')) %>% names()

# set the values to look at
mtcars_numbs <- c(19.2, 6)

# result is 8 vars with either value in either col.
# this is a successful filter of the data
out1 <- mtcars %>% 
    filter_at(vars(mtcars_cols), any_vars(
        . %in% mtcars_numbs
        )
      )

# shows set with all 6 cyl, plus one 8cyl 21.9 mpg
out1 %>% 
  select(mpg, cyl)

# This attempts to apply the filter list to the cols,
# but I only get 6 rows as True
# I tried to change == to %in& but that results in an error
out2 <- mtcars %>%
    mutate(
      myset = rowSums(select(., mtcars_cols) == mtcars_numbs) > 0
    )

# only 6 rows returned
out2 %>% 
  filter(myset == T)
#共有7辆车,6个气缸
mtcars%>%
过滤器(气缸==6)
#共有2辆车,每加仑19.2英里,一辆6缸,一辆8缸
mtcars%>%
过滤器(mpg==19.2)
#有8行,其中有一行。
#这些是我想要的真实的行
mtcars%>%
过滤器(mpg==19.2 |气缸==6)
#让孩子们看看
mtcars\u cols%
选择(匹配(“^(mp|cy)”)%>%names()
#设置要查看的值
mtcars\U numbs%
选择(mpg,气缸)
#这将尝试将筛选器列表应用于COL,
#但我只有6行是真的
#我试图将==更改为%in&但这导致了一个错误
出局2%
变异(
myset=rowSums(选择(,mtcars\u cols)==mtcars\u numbs)>0
)
#只返回了6行
输出2%>%
过滤器(myset==T)

我不知道为什么跳过这两行。我认为可能是使用了
rowSums
,以某种方式将这两行相加

如果我们想进行相应的检查,最好使用
map2

 library(dplyr)
 library(purrr)
 map2_df(mtcars_cols, mtcars_numbs, ~ 
       mtcars %>%
           filter(!! rlang::sym(.x) == .y)) %>%
     distinct
注意:使用浮点数进行比较(
==
)可能会遇到问题,因为精度可能会发生变化并导致错误


另外,请注意,
=
仅当
lhs
rhs
元素具有相同的长度或
rhs
向量具有
长度
1时才起作用(此处发生循环)。如果
长度
大于1且不等于lhs向量的长度,则循环将按列顺序进行比较

我们可以
rep
licate使长度相等,现在应该可以工作了

mtcars %>%
 mutate(
   myset = rowSums(select(., mtcars_cols) == mtcars_numbs[col(select(., mtcars_cols))]) > 0
   ) %>% pull(myset) %>% sum
#[1] 8
在上述代码中,为了更好地理解,使用了两次
select
。否则,我们也可以使用
rep

mtcars %>%
 mutate(
   myset = rowSums(select(., mtcars_cols) == rep(mtcars_numbs, each = n())) > 0
    ) %>% 
   pull(myset) %>%
   sum
#[1] 8

关于浮点数的观点很好,尽管我在实际数据集中搜索字符串而不是数字。。。我应该使用不同的示例数据集。我今天要试一试,让你知道。我的另一个想法是构建一个函数,在值的列列表中循环,并用mutate设置答案。好的,我承认我在尝试将
map2
概念应用到数据时有点迷茫。我还觉得使用mtcars和浮点数据与我的实际挑战不太相似,所以我有一个很长的解释,说明了我的目标和我的尝试。我将在上面添加一些编辑以反映这一点。