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

时间:2022-05-03 09:17:51

I'd like to pass argument (stringsAsFactors=FALSE) to rbind in do.call. But the following doesn't work:

我想在do.call中将参数(stringsAsFactors = FALSE)传递给rbind。但以下不起作用:

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

3 个解决方案

#1


19  

do.call(rbind.data.frame, c(list(iris), list(iris), stringsAsFactors=FALSE))

would have been my answer, if it wasn't for the fact that rbind does not know what to do with stringsAsFactors (but cbind.data.frame would).

本来是我的答案,如果不是因为rbind不知道如何处理stringsAsFactors(但cbind.data.frame会)。

The output of strsplit is presumably a list of vectors, in which case rbind creates a matrix. You can specify stringsAsFactors when converting this matrix to a data.frame,

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

data.frame(do.call(rbind, list(1:10, letters[1:10])), stringsAsFactors=FALSE)

#2


6  

Alternatively, you can set stringsAsFactors to FALSE globally using options:

或者,您可以使用选项全局设置stringsAsFactors为FALSE:

options(stringsAsFactors=FALSE)

Setting this at the top of the script will enforce this throughout the script. You could even add to .Rprofile to set this option for the all the R sessions you open.

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

#3


1  

I'm not sure if your function call is valid, but try this:

我不确定你的函数调用是否有效,但试试这个:

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

You need pass all arguments to do.call via one list. You can concat two list by c

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

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

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

#1


19  

do.call(rbind.data.frame, c(list(iris), list(iris), stringsAsFactors=FALSE))

would have been my answer, if it wasn't for the fact that rbind does not know what to do with stringsAsFactors (but cbind.data.frame would).

本来是我的答案,如果不是因为rbind不知道如何处理stringsAsFactors(但cbind.data.frame会)。

The output of strsplit is presumably a list of vectors, in which case rbind creates a matrix. You can specify stringsAsFactors when converting this matrix to a data.frame,

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

data.frame(do.call(rbind, list(1:10, letters[1:10])), stringsAsFactors=FALSE)

#2


6  

Alternatively, you can set stringsAsFactors to FALSE globally using options:

或者,您可以使用选项全局设置stringsAsFactors为FALSE:

options(stringsAsFactors=FALSE)

Setting this at the top of the script will enforce this throughout the script. You could even add to .Rprofile to set this option for the all the R sessions you open.

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

#3


1  

I'm not sure if your function call is valid, but try this:

我不确定你的函数调用是否有效,但试试这个:

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

You need pass all arguments to do.call via one list. You can concat two list by c

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

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

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4