How to use acast (reshape2) within a function in R?

后端 未结 4 1504
感动是毒
感动是毒 2021-02-09 10:40

I tried to use acast from reshape2 within a self written function, but had the problem that acast did not find the data I send to it.

Here is m

4条回答
  •  有刺的猬
    2021-02-09 11:21

    I found a pretty inelegant way to solve the problem using super assignments (<<-).
    Changing the function to the following does the job. But, it is pretty ugly as it creates global variables which remain.

    wrap.acast <- function(dat, v1 = 1, v2 = 2) {
        dat <<- dat
        v1 <<- v1
        v2 <<- v2
        out <- acast(dat, dat[,v1] ~ dat[,v2])
        return(out)
    }
    

    I am still very interested in other (less clogging) solutions.

    prior to running the function:

    > ls()
    [1] "wrap.acast" "x"          "y"     
    

    after running the function:

    > ls()
    [1] "dat"        "v1"         "v2"         "wrap.acast" "x"         
    [6] "y" 
    

提交回复
热议问题