Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
dplyr::根据属性()中存储的信息选择_R_Dplyr - Fatal编程技术网

dplyr::根据属性()中存储的信息选择

dplyr::根据属性()中存储的信息选择,r,dplyr,R,Dplyr,让我们创建一个tibble,添加一个属性,该属性包含以后要选择的变量 mytib<-tibble(aa=1:10,bb=21:30,cc=letters[1:10],dd=letters[11:20]) attributes(mytib)$select_me <- c('bb','cc') 好吧,那不管用,但你明白了 编写一个基于属性信息返回逻辑向量的函数很容易,但如何将其转换为select(理想情况下是FP)样式 myfun=function(x)names(x) %in% at

让我们创建一个tibble,添加一个属性,该属性包含以后要选择的变量

mytib<-tibble(aa=1:10,bb=21:30,cc=letters[1:10],dd=letters[11:20])
attributes(mytib)$select_me <- c('bb','cc')
好吧,那不管用,但你明白了

编写一个基于属性信息返回逻辑向量的函数很容易,但如何将其转换为select(理想情况下是FP)样式

myfun=function(x)names(x) %in% attributes(x)$select_me
myfun(mytib)
这个怎么样

mytib %>% 
  select(
    one_of(attributes(mytib)$select_me)
  )

这是可行的,但需要在
select
/one\u中明确提及
mytib
。有没有办法在这里使用匿名函数?也许这更接近你想要的?只需编写自己的函数即可:
my_select=function(df){df[,attributes(df)[['select_me']]};mytib%>%my_select
它可以完成任务。我仍然很好奇,想知道在
select\x
中是否可以做一些事情。问题是:
myf=function(x){test
mytib %>% 
  select(
    one_of(attributes(mytib)$select_me)
  )