简体   繁体   中英

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)

I tried to set that String as an argument into the following Function:

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)

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| 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...

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 |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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