Return a list in dplyr mutate()

后端 未结 3 1105
小鲜肉
小鲜肉 2020-12-17 16:03

I have a function in my real-world problem that returns a list. Is there any way to use this with the dplyr mutate()? This toy example doesn\'t work -:

it =          


        
3条回答
  •  情话喂你
    2020-12-17 16:46

    The idiomatic way to do this using data.table would be to use the := (assignment by reference) operator. Here's an illustration:

    it[, c(paste0("V", 4:5)) := myfun(V2, V3)]
    

    If you really want a list, why not:

    as.list(it[, myfun(V2, V3)])
    

    Alternatively, maybe this is what you want, but why don't you just use the data.table functionality:

    it[, c(.SD, myfun(V2, V3))]
    #    V1 V2 V3 V4 V5
    # 1:  a  1  2  3 -1
    # 2:  a  2  3  5 -1
    # 3:  b  3  4  7 -1
    # 4:  b  4  2  6  2
    # 5:  c  5  2  7  3    
    

    Note that if myfun were to name it's output, then the names would show up in the final result columns:

    #    V1 V2 V3 new.1 new.2
    # 1:  a  1  2     3    -1
    # 2:  a  2  3     5    -1
    # 3:  b  3  4     7    -1
    # 4:  b  4  2     6     2
    # 5:  c  5  2     7     3    
    

提交回复
热议问题