Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用三点参数时,list(…)与as.list(…)_R_List_Arguments - Fatal编程技术网

R 使用三点参数时,list(…)与as.list(…)

R 使用三点参数时,list(…)与as.list(…),r,list,arguments,R,List,Arguments,我想得到一个带有传递给函数的“…”参数的列表 myfunction <- function(..., a=1){ parameters <- as.list(...) for(i in parameters){ print(i) } } as.list(c('x', 'y', 'z')) #[[1]] #[1] "x" # #[[2]] #[1] "y" # #[[3]] #[1] "z" myfunction <- function(..., a=1)

我想得到一个带有传递给函数的“…”参数的列表

myfunction <- function(..., a=1){
  parameters <- as.list(...)
  for(i in parameters){
    print(i)
  }
}
as.list(c('x', 'y', 'z'))
#[[1]]
#[1] "x"
#
#[[2]]
#[1] "y"
#
#[[3]]
#[1] "z"

myfunction <- function(..., a=1){
  parameters <- as.list(c(...))
  for(i in parameters){
    print(i)
  }
}

myfunction('x', 'y', 'z')
#[1] "x"
#[1] "y"
#[1] "z"
但是,如果我将
as.list(…)
替换为
list(…)

那么为什么
as.list(…)
的行为会有所不同呢


干杯。

您可能正在寻找
c
串联函数

myfunction <- function(..., a=1){
  parameters <- as.list(...)
  for(i in parameters){
    print(i)
  }
}
as.list(c('x', 'y', 'z'))
#[[1]]
#[1] "x"
#
#[[2]]
#[1] "y"
#
#[[3]]
#[1] "z"

myfunction <- function(..., a=1){
  parameters <- as.list(c(...))
  for(i in parameters){
    print(i)
  }
}

myfunction('x', 'y', 'z')
#[1] "x"
#[1] "y"
#[1] "z"
as.list(c('x','y','z'))
#[[1]]
#[1] “x”
#
#[[2]]
#[1] “y”
#
#[[3]]
#[1] “z”

myfunction三点参数与行为无关,比较
as.list('x','y','z')
list('x','y','z')
的结果。将
data.frame('x','y')
vs
as.data.frame('x','y')
as.list
接受一个
x
参数和
传递给其他方法的参数
as.list(“x”、“y”、“z”)
忽略“y”和“z”,因为它们被视为未使用的参数,并将“x”(即
x
参数)强制为“列表”<代码>列表
接受
参数并在其上构建一个“列表”。
as.list(c('x', 'y', 'z'))
#[[1]]
#[1] "x"
#
#[[2]]
#[1] "y"
#
#[[3]]
#[1] "z"

myfunction <- function(..., a=1){
  parameters <- as.list(c(...))
  for(i in parameters){
    print(i)
  }
}

myfunction('x', 'y', 'z')
#[1] "x"
#[1] "y"
#[1] "z"