How to pass extra argument to the function argument of do.call in R

前端 未结 3 1638
北恋
北恋 2021-02-06 21:29

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

data <- do.ca         


        
相关标签:
3条回答
  • 2021-02-06 22:00
    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).

    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,

    data.frame(do.call(rbind, list(1:10, letters[1:10])), stringsAsFactors=FALSE)
    
    0 讨论(0)
  • 2021-02-06 22:10

    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

    > c(list(1, 2), list(3, 4))
    [[1]]
    [1] 1
    
    [[2]]
    [1] 2
    
    [[3]]
    [1] 3
    
    [[4]]
    [1] 4
    
    0 讨论(0)
  • 2021-02-06 22:19

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

    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.

    0 讨论(0)
提交回复
热议问题