用 R 中的变量定义 $right 参数 [英] define $ right parameter with a variable in R

查看:54
本文介绍了用 R 中的变量定义 $right 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个变量传递给二元运算符 $.

I would like to pass a variable to the binary operator $.

假设我有这个

> levels(diamonds$cut)
[1] "Fair"      "Good"      "Very Good" "Premium"   "Ideal" 

然后我想创建一个函数,它以 $

Then I want to make a function that takes as parameter the selector for $

my_helper <- function (my_param) {
  levels(diamonds$my_param)
}

但这不起作用

> my_helper(cut)
NULL

> my_helper("cut")
NULL

推荐答案

使用 [[ 而不是 $.x$yx[["y"]] 的简写.

Use [[ instead of $. x$y is short hand for x[["y"]].

my_helper <- function (my_param) {
  levels(diamond[[my_param]])
}
my_helper("cut")

这篇关于用 R 中的变量定义 $right 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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