简体   繁体   English

使用字符串作为函数参数R

[英]Use character String as function argument R

I lost already so much time but I don't get it. 我已经浪费了很多时间,但我不明白。

Is it possible to use a String as an argument in a function? 是否可以在函数中使用字符串作为参数?

My String is definded as: 我的字串定义为:

mergesetting <- "all = FALSE"

(Sometimes I use "all.y = TRUE" or "all.x = TRUE" instead) (有时我改用“ all.y = TRUE”或“ all.x = TRUE”)

I tried to set that String as an argument into the following Function: 我试图将String设置为以下函数的参数:

merged = merge.data.frame(x = DataframeA ,y = DataframeB ,by = "date_new", mergesetting )

But i get an error message: Error in fix.by(by.x, x) 但是我收到一条错误消息:fix.by(by.x,x)中的错误

The function does work if I use the argument directly: 如果我直接使用参数,该函数可以正常工作:

merged = merge.data.frame(x = DataframeA,y = DataframeB,by = "date_new", all = FALSE )

As well two other approaches found on Use character string as function argument didn't work: 同样,在使用字符串作为函数参数中找到的其他两种方法也无效:

  L<- list(x = DataframeA,y = DataframeB,by = "date_new", mergesetting)
  merged <- do.call(merge.data.frame, L)

Any help is much appreciated. 任何帮助深表感谢。

Not sure the point but, if you had a list with your data and arguments 不确定要点,但是,如果您有包含数据和参数的列表

Say dfA is this data frame kable(head(dfA)) |dates | datum| |:----------|-----:| |2010-05-11 | 1130| |2010-05-12 | 1558| |2010-05-13 | 1126| |2010-05-14 | 131| |2010-05-15 | 2223| |2010-05-16 | 4005| 说dfA是此数据帧kable(head(dfA)) |dates | datum| |:----------|-----:| |2010-05-11 | 1130| |2010-05-12 | 1558| |2010-05-13 | 1126| |2010-05-14 | 131| |2010-05-15 | 2223| |2010-05-16 | 4005| kable(head(dfA)) |dates | datum| |:----------|-----:| |2010-05-11 | 1130| |2010-05-12 | 1558| |2010-05-13 | 1126| |2010-05-14 | 131| |2010-05-15 | 2223| |2010-05-16 | 4005|

and dfB is this... dfB是这个...

kable(head(dfB)) |dates | datum| |:----------|-----:| |2010-05-11 | 3256| |2010-05-12 | 50| |2010-05-13 | 2280| |2010-05-14 | 4981| |2010-05-15 | 2117| |2010-05-16 | 791|

Your pre set list: 您的预设清单:

arg.li <- list(dfA = dfA,dfB = dfB,all = T,by = 'dates')

The wrapper for the list function... 列表功能的包装...

f <- function(x)do.call('merge.data.frame',list(x = x$dfA,y = x$dfB,all = x$all))

results in: 结果是:

kable(summary(f(arg.li))) | | dates | datum | |:--|:------------------|:------------| | |Min. :2010-05-11 |Min. : 24 | | |1st Qu.:2010-09-03 |1st Qu.:1288 | | |Median :2010-12-28 |Median :2520 | | |Mean :2011-01-09 |Mean :2536 | | |3rd Qu.:2011-04-22 |3rd Qu.:3785 | | |Max. :2011-12-01 |Max. :5000 |

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

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