如何在函数中访问/使用R对象的名称? [英] How do I access/use the name of an R object in a function?

查看:162
本文介绍了如何在函数中访问/使用R对象的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将需要创建一些参考表,其中指定了给定课程和时间段,学生应达到的学分数量。

I will need to create a number of reference tables where I specify the number of academic credits a student is supposed to have to achieved, given a certain programme and time period.

在数据框 fulldata中,我为每个学生列出了开始日期和课程。
作为第一步,我想使用此数据框来获取某个程序的唯一开始日期的列表。理想情况下,由于有很多程序,我将能够通过一个函数使此步骤(及后续步骤)自动化。

I have a list of start dates and programmes for each individual student in the data frame "fulldata". As a first step, I want to use this data frame to get a list of unique start dates for a certain programme. Ideally, I would be able to automate this (and later) step(s) via a function since there are a lot of programmes.

目前,我有三个程序,经济学(经济学),房地产和数字媒体。我有三个对应的R对象(依次包含每个模块的学分矢量),分别称为 Ekonomi,房地产和数字媒体。我想从 fulldata中获取start_date的唯一值,其中程序名称等于我当前R对象的名称

At present I have three programmes, Economics(Ekonomi), Real Estate and Digital Media. I have three corresponding R-objects (containing a vector of academic credits for each module in order) named "Ekonomi", "Real Estate" and "Digital media". I want to fetch the unique values of start_date from "fulldata" where the program name equals the name of my current R object

我写:

start_dates<-function(x){
sd<-fulldata%>%filter(program==deparse(substitute(x)))%>%
dplyr::select(UTBILDNINGSTILLFALLE_STARTDATUM)%>%drop_na()%>%unique()
}

因此对于 start_dates(Ekonomi),该函数应获取与程序 Ekonomi相等的观测值的开始日期。但是,这似乎不起作用。

So for start_dates(Ekonomi) the function should fetch the start dates for observations with programme equal to "Ekonomi". This does not seem to work however.

我写作时

start_dates(Ekonomi)
sd

事实证明,sd不包含任何观测值。

It turns out that sd does not contain any observations.

我可以这样写:

sd<-fulldata%>%filter(program==deparse(substitute(Ekonomi)))%>%
dplyr::select(UTBILDNINGSTILLFALLE_STARTDATUM)%>%drop_na()%>%unique()
}

....然后sd完全可以,但我似乎无法使用函数来完成同样的事情。

....and then sd turns out completely fine, but I don't seem to be able to do the same thing with a function.

我在做什么错了,我该怎么做?

What am I doing wrong and how can I make this work?

Small exerpt of data:

structure(list(UTBILDNINGSTILLFALLE_STARTDATUM = structure(c(15586, 
15586, 15586, 15586, 15586, 15586, 15586, 15586, NA, 15586, 15586, 
NA, 15586, 15586, 15586, NA, 15586, 15586, 15586, 15586), class = "Date"), 
    program = c("Ekonom", "Mäklarekonom", "Ekonom", "Mäklarekonom", 
    "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Mäklarekonom", "Ekonom", 
    "Ekonom", "Mäklarekonom", "Ekonom", "Ekonom", "Mäklarekonom", 
    "Mäklarekonom", "Ekonom", "Ekonom", "Mäklarekonom", "Mäklarekonom"
    )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-20L))


推荐答案

如果要传递未引用的变量进行过滤,一种方法是使用 rlang :: enexpr(x)

If you want to pass an unquoted variable to filter, one way is to use rlang::enexpr(x)

library(dplyr)

start_dates<-function(fulldata, x){

  fulldata%>%
    filter(program == as.character(rlang::enexpr(x))) %>%
    distinct(UTBILDNINGSTILLFALLE_STARTDATUM)
}

start_dates(full_data, Ekonom)

# A tibble: 1 x 1
#  UTBILDNINGSTILLFALLE_STARTDATUM
#  <date>                         
#1 2012-09-03             

传递带引号的变量不需要任何这些并且可以直接作为

Passing a quoted variable would not require any of these and can be directly done as

start_dates<-function(fulldata, x){
   fulldata%>%
     filter(program == x) %>%
     distinct(UTBILDNINGSTILLFALLE_STARTDATUM)
}

start_dates(full_data, "Ekonom")

这篇关于如何在函数中访问/使用R对象的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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