为什么expand.grid会忽略选项?

时间:2014-06-04 22:04:20

标签: r

我遇到expand.grid的问题。它似乎忽略了我在脚本顶部设置的选项。

例如:

options(stringsAsFactors = FALSE)
tmp <- expand.grid(x = letters, y = 1:10)

返回:

> str(tmp)
'data.frame':   260 obs. of  2 variables:
 $ x: Factor w/ 26 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ y: int  1 1 1 1 1 1 1 1 1 1 ...
 - attr(*, "out.attrs")=List of 2
  ..$ dim     : Named int  26 10
  .. ..- attr(*, "names")= chr  "x" "y"
  ..$ dimnames:List of 2
  .. ..$ x: chr  "x=a" "x=b" "x=c" "x=d" ...
  .. ..$ y: chr  "y= 1" "y= 2" "y= 3" "y= 4" ...

我做错了什么?

2 个答案:

答案 0 :(得分:15)

这是因为expand.grid的函数参数default设置为TRUE。如果您只是从R会话中键入?expand.gridhead(expand.grid),您会看到函数定义为:

> head(expand.grid)

1 function (..., KEEP.OUT.ATTRS = TRUE, stringsAsFactors = TRUE) 
2 {                                                              
3     nargs <- length(args <- list(...))                         
4     if (!nargs)                                                
5         return(as.data.frame(list()))                          
6     if (nargs == 1L && is.list(a1 <- args[[1L]]))

这与提供给read.table()的默认值不同,例如:

> head(read.table)
1 function (file, header = FALSE, sep = "", quote = "\\"'", dec = ".",        
2     row.names, col.names, as.is = !stringsAsFactors, na.strings = "NA",     
3     colClasses = NA, nrows = -1, skip = 0, check.names = TRUE,              
4     fill = !blank.lines.skip, strip.white = FALSE, blank.lines.skip = TRUE, 
5     comment.char = "#", allowEscapes = FALSE, flush = FALSE,                
6     stringsAsFactors = default.stringsAsFactors(), fileEncoding = "",    

default.stringsAsFactors()基本上会返回getOption("stringsAsFactors")

因此,您必须明确设置它。

答案 1 :(得分:14)

除了@Arun解释之外,您还可以打包expand.grid

 expand_grid <- 
   function(...,stringsAsFactors= getOption("stringsAsFactors"))
     expand.grid(...,stringsAsFactors=stringsAsFactors)

现在,如果您应用新功能,您将获得所需的类型:

options(stringsAsFactors = FALSE)
tmp <- expand_grid(x = letters, y = 1:10)
str(tmp,max=1)
## 'data.frame':    260 obs. of  2 variables:
## $ x: chr  "a" "b" "c" "d" ...
## $ y: int  1 1 1 1 1 1 1 1 1 1 ...
## - attr(*, "out.attrs")=List of 2