在函数内部调用sep =“”

问题描述 投票:0回答:1

所以,我想在其中的参数do.call(paste)中使用sep =“”。

fun <- function(x, sep=" "){
  stopifnot(is.list(x))
  k <- length(x)
  n <- lengths(x)
  stopifnot(length(unique(n))==1)
  do.call(paste, c(x, sep))
}

[不幸的是,这没有用,我找不到任何类似的主题。感谢您的帮助:)

r paste do.call
1个回答
0
投票

[我认为您可能需要sep = sep中的do.call,因为lhs seppaste的,而rhs sep是函数fun的输入自变量,即

fun <- function(x, sep=" "){
  stopifnot(is.list(x))
  k <- length(x)
  n <- lengths(x)
  stopifnot(length(unique(n))==1)
  do.call(paste, c(x, list(sep=sep)))
}

示例

> fun(as.list(seq(3)),"-")
[1] "1-2-3"
© www.soinside.com 2019 - 2024. All rights reserved.