Generate combination of data frame and vector

后端 未结 3 576
一整个雨季
一整个雨季 2021-01-18 08:46

I know expand.grid is to create all combinations of given vectors. But is there a way to generate all combinations of a data frame and a vector by taking each r

3条回答
  •  难免孤独
    2021-01-18 09:22

    You could also do something like this

    do.call(rbind,lapply(9:10, function(x,d) data.frame(d, c=x), d=df)))
    
    # or using rbindlist as a fast alternative  to do.call(rbind,list)
    library(data.table)
    rbindlist(lapply(9:10, function(x,d) data.frame(d, c=x), d=df)))
    

    or

    rbindlist(Map(data.frame, c = 9:10, MoreArgs = list(a= 1:3,b=5:7)))
    

提交回复
热议问题