使用粘贴并与R中的引号组合替换

编程入门 行业动态 更新时间:2024-10-14 10:42:00
本文介绍了使用粘贴并与R中的引号组合替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请注意,我已经看过这和 但仍然无法解决我的问题。

假设一个最简单的工作示例:

a <-c(1,2,3)b <-c(2,3,4)c <-c(4,5,6)$ b $ (d,b,c) foo< - 函数(x,y,data = data){ data [,c(x ,y)] } foo(a,b,data = dftest)

在这里,最后一行显然会返回一个错误:未定义的列被选中。返回此错误是因为要选择的列是 x 和 y ,它们不是数据框的一部分 dftest 。

问题:如何制定函数的定义以获得所需的输出,即

> dftest [,c(a,b)] #ab #1 1 2 #2 2 3 #3 3 4

我想通过调用函数 foo 来获得。

请注意,为了使解决方案对我的目的有用,函数调用格式 foo 被认为是固定的,即只对函数本身做出改变,而不是调用。即 foo(a,b,data = dftest)是唯一允许的输入。

与 eval 结合使用粘贴和替换到第一个用函数调用的参数替换 x 和 y ,然后评估调用。但是,转义引号在这里似乎是一个问题:

foo < - function(x,y,data = data ){ substitute(data [,paste(c(\,x,\,\,y,\),sep =)]) foo(a,b,data = dftest) eval(foo(a,b,data = dftest)) 这里, foo(a,b,data = dftest)返回:

dftest [,paste(c(\,a,\,\,b,\)),sep然而,当使用 eval()来评估时, code>(仅限于粘贴部分),

paste(c(\,a,\,\,b,\),sep =) pre>

返回:

#c(\1 \2 \)c(\2 \,\3 \)c(\3 \,\4 \ )

而不是,因为我希望 c(a ,b),因此再次如上所述的错误。

解决方案

试试这个: <$ (x,y,data = data){x< - deparse(substitute(x))y< - deparse(substitute( y)) data [,c(x,y)] }

Please note that I already had a look at this and that but still cannot solve my problem.

Suppose a minimal working example:

a <- c(1,2,3) b <- c(2,3,4) c <- c(4,5,6) dftest <- data.frame(a,b,c) foo <- function(x, y, data = data) { data[, c("x","y")] } foo(a, b, data = dftest)

Here, the last line obviously returns an Error: undefined columns selected. This error is returned because the columns to be selected are x and y, which are not part of the data frame dftest.

Question: How do I need to formulate the definition of the function to obtain the desired output, which is

> dftest[, c("a","b")] # a b # 1 1 2 # 2 2 3 # 3 3 4

which I want to obtain by calling the function foo.

Please be aware that in order for the solution to be useful for my purposes, the format of the function call of foo is to be regarded fixed, that is, the only changes are to be made to the function itself, not the call. I.e. foo(a, b, data = dftest) is the only input to be allowed.

Approach: I tried to use paste and substitute in combination with eval to first replace the x and y with the arguments of the function call and then evaluate the call. However, escaping the quotation marks seems to be a problem here:

foo <- function(x, y, data = data) { substitute(data[, paste("c(\"",x,"\",\"",y,"\")", sep = "")]) } foo(a, b, data = dftest) eval(foo(a, b, data = dftest))

Here, foo(a, b, data = dftest) returns:

dftest[, paste("c(\"", a, "\",\"", b, "\")", sep = "")]

However, when evaluating with eval() (focusing only on the paste part),

paste("c(\"", a, "\",\"", b, "\")", sep = "")

returns:

# "c(\"1\",\"2\")" "c(\"2\",\"3\")" "c(\"3\",\"4\")"

and not, as I would hope c("a","b"), thus again resulting in the same error as above.

解决方案

Try this:

foo <- function(x, y, data = data) { x <- deparse(substitute(x)) y <- deparse(substitute(y)) data[, c(x, y)] }

更多推荐

使用粘贴并与R中的引号组合替换

本文发布于:2023-11-30 13:56:53,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   引号   并与

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!