如何将额外参数传递给R中do.call的函数参数

zjf*_*fdu 36 r

我想通过参数(stringsAsFactors=FALSE),以rbinddo.call.但以下不起作用:

data <- do.call(rbind, 
          strsplit(readLines("/home/jianfezhang/adoption.txt"), split="\t#\t"), 
          args=list(stringsAsFactors=FALSE))
Run Code Online (Sandbox Code Playgroud)

bap*_*ste 28

do.call(rbind.data.frame, c(list(iris), list(iris), stringsAsFactors=FALSE))
Run Code Online (Sandbox Code Playgroud)

本来是我的答案,如果不是因为rbind不知道该怎么做stringsAsFactors(但cbind.data.frame愿意).

输出strsplit可能是矢量列表,在这种情况下rbind会创建一个矩阵.您可以指定stringsAsFactors何时将此矩阵转换为data.frame,

data.frame(do.call(rbind, list(1:10, letters[1:10])), stringsAsFactors=FALSE)
Run Code Online (Sandbox Code Playgroud)


Pau*_*tra 7

或者,您可以设置stringsAsFactorsFALSE全局使用options:

options(stringsAsFactors=FALSE)
Run Code Online (Sandbox Code Playgroud)

在脚本顶部设置此选项将在整个脚本中强制执行此操作.您甚至可以添加.Rprofile以为您打开的所有R会话设置此选项.


koh*_*ske 6

我不确定您的函数调用是否有效,但请尝试以下操作:

data <- do.call(rbind,
  c(strsplit(readLines("/home/jianfezhang/adoption.txt"),split="\t#\t"),
  list(stringsAsFactors=FALSE))
Run Code Online (Sandbox Code Playgroud)

您需要通过一个列表传递所有参数do.call。您可以通过以下方式连接两个列表c

> c(list(1, 2), list(3, 4))
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4
Run Code Online (Sandbox Code Playgroud)