简体   繁体   English

在函数中调用$

[英]Calling $ within a function

This feels like a very beginner question. 这感觉像是一个初学者的问题。

In R, is there any way to use the $ operator based on parameters passed to a function? 在R中,是否可以根据传递给函数的参数使用$运算符?

I'm trying to write a simple function: 我正在尝试编写一个简单的函数:

subs <- function(x, y){
  x<- subset(x, x$UR!=0)
  x<- subset(x, !is.na(x$y))
  x
}

This returns: 返回:

Warning message:
In is.na(x$y) : is.na() applied to non-(list or vector) of type 'NULL'

Because I have over 30 columns, counting it out to do this with [y] would not be useful. 因为我有30多个列,所以用[y]进行计数将无济于事。

Is there any way to dynamically call using $ within a function? 有什么方法可以在函数中使用$动态调用?

Thank you. 谢谢。

Expanding on @akrun's comment: 扩展@akrun的评论:

subs <- function(x, y){
  x <- x[x[,"UR"]!=0,]
  x <- x[!is.na(x[,y]),]
  x
}

Where your call to the function would look like subs(dataframe, "variable.name") , with the latter in quotes. 您对函数的调用应类似于subs(dataframe, "variable.name") ,后者用引号引起来。

The expression inside the brackets in each of the first two lines returns a vector of TRUE/FALSE values identifying the rows in x that satisfy the specified condition (eg, for row i in x, the value of column "UR" is not equal to 0). 前两行的方括号内的表达式返回一个TRUE / FALSE值的向量,该值标识x中满足指定条件的行(例如,对于x中的行i,“ UR”列的值不等于0)。 Putting that in brackets adjacent to x with a comma after the expression uses that vector to select from x only those rows (hence the trailing comma) for which the result of the nested expression is TRUE. 在表达式之后,用逗号将其放在与x相邻的括号中,使用该向量从x中仅选择嵌套表达式的结果为TRUE的那些行(因此,以逗号结尾)。

NB This answer assumes that UR is the name of a variable in that dataframe. 注意:此答案假定UR是该数据帧中变量的名称。

In general, when there's a special operator like * or + or %% or $ or [ (etc), the first step in using or accessing more information about that operator is to put it in quotes. 通常,当有一个特殊的运算符,例如*+%%$[ (等)时,使用或访问有关该运算符的更多信息的第一步是将其放在引号中。 You want the help page for $ ? 您想要$的帮助页面吗? Then do ?"$" . 然后?"$" Now, if you want to use that function in a non-standard fashion (as you want to do, and this is just fine), then you can make use to of do.call , and the first argument to that function is a function in quotes: do.call(what="$", ...) The next arguments to do.call involve a list of arguments to be passed to the what . 现在,如果您想以非标准的方式使用该函数(如您所愿,这很好),则可以使用do.call to,并且该函数的第一个参数是一个函数用引号引起来: do.call(what="$", ...)接下来的do.call参数涉及要传递给what的参数列表。 This is much broad than what you asked, but I hope it is useful to you in the future. 这比您的要求要广泛得多,但是我希望它对您将来有用。

Second, you don't need to use $ . 其次,您不需要使用$ You can just specify a column name for your data.frame. 您可以只为data.frame指定列名。 Instead of data$col , try data[,"col"] . 尝试使用data[,"col"]代替data$col For a data.frame, those are the same. 对于data.frame,这些相同。 If data was a list, you could do data[["col"]] . 如果data是一个列表,则可以执行data[["col"]]

Here are examples that specifically address your question: 以下是专门解决您的问题的示例:

# Data set to work with for examples
df <- data.frame(ran=rnorm(10), num=1:10)

# Function giving example of what you wanted
get.col <- function(dat, col){
    do.call("$", list(dat, col))
}
get.col(df, "ran")

# A function providing an alternative approach
get.col2 <- function(dat, col){
    dat[,col]
}
get.col2(df, "ran")

You can use column names as follows: 您可以按如下方式使用列名:

Fist I load example data, 我先加载示例数据,

data(mtcars) head(mtcars data(mtcars) head(mtcars

                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1

Re-write the function: 重新编写函数:

subs <- function(x){
  x<- subset(x, x[,"mpg"]!=21.0)
  x<- subset(x,!is.na(x[,"mpg"]))
  x
}


subs(mtcars)
                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM