dplyr :: select()具有某些可能在数据框中不存在的变量? [英] dplyr::select() with some variables that may not exist in the data frame?

查看:197
本文介绍了dplyr :: select()具有某些可能在数据框中不存在的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个辅助函数(例如foo()),它将在可能包含或不包含指定变量的各种数据帧上运行.假设我有

I have a helper function (say foo()) that will be run on various data frames that may or may not contain specified variables. Suppose I have

library(dplyr)
d1 <- data_frame(taxon=1,model=2,z=3)
d2 <- data_frame(taxon=2,pss=4,z=3)

我要选择的变量是

vars <- intersect(names(data),c("taxon","model","z"))

也就是说,我希望foo(d1)返回taxonmodelz列,而foo(d2)仅返回taxonz.

that is, I'd like foo(d1) to return the taxon, model, and z columns, while foo(d2) returns just taxon and z.

如果foo包含select(data,c(taxon,model,z)),则foo(d2)失败(因为d2不包含model).如果我使用select(data,-pss),则foo(d1)同样会失败.

If foo contains select(data,c(taxon,model,z)) then foo(d2) fails (because d2 doesn't contain model). If I use select(data,-pss) then foo(d1) fails similarly.

我知道如果我从tidyverse撤退(只是返回data[vars]),该怎么做,但是我想知道是否有一种便捷的方法(1)和某种select()助手( tidyselect::select_helpers)或(2)与tidyeval(我仍然还没来得及动手!)

I know how to do this if I retreat from the tidyverse (just return data[vars]), but I'm wondering if there's a handy way to do this either (1) with a select() helper of some sort (tidyselect::select_helpers) or (2) with tidyeval (which I still haven't found time to get my head around!)

推荐答案

另一个选项是select_if:

d2 %>% select_if(names(.) %in% c('taxon', 'model', 'z'))

# # A tibble: 1 x 2
#   taxon     z
#   <dbl> <dbl>
# 1     2     3

这篇关于dplyr :: select()具有某些可能在数据框中不存在的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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