Search code examples
rrmongodb

rmongodb - combine two data frames into one document in a collection


Question

Using R and rmongodb, how do I create a mongodb document from two data frames, the second of which will be an array element of the first?

Data

My first data.frame is always one row. e.g.

df_1 <- data.frame(myVar1 = 1,
                   myVar2 = 2,
                   myVar3 = 3)

My second data.frame is always one or more rows e.g.

df_2 <- data.frame(arrVar1 = c(1,2),
                   arrVar2 = c(1,2))

Required Solution

my goal is to have a document in a collection that structured like:

# {
# "_id" : ObjectId("565a939aa30fff2d67bfd492"),
# "vars" : {
#   "myVar1" : 1.0000000000000000,
#   "myVar2" : 2.0000000000000000,
#   "myVar3" : 3.0000000000000000,
#   "myArr" : [
#        {
#            "arrVar1" : 1,
#            "arrVar2" : 1
#         },
#         {
#            "arrVar1" : 2,
#            "arrVar2" : 2
#         }
#     ]
#   }  
# }

How can I achieve this?


Edit

(removed all my attempts)

Thanks to Dmitriy for the answer and showing me what structure I needed to achieve.

As such, I've benchmarked a few different ways of getting the solution.

library(microbenchmark)

fun_1 <- function(df){
  list(myArr = unname(split(df, seq(nrow(df)))))  
}

fun_2 <- function(df){
  list('myArr' = Map(function(i, d) d[i, ], 
                     seq_len(nrow(df)), 
                     MoreArgs = list('d' = df)
  ))
}

fun_3 <- function(df){
  list(myArr = (lapply(as.list(1:dim(df)[1]), function(x) df[x[1],])))
}

microbenchmark(fun_1(df_2), fun_2(df_2), fun_3(df_2),  times = 1000)


Unit: microseconds
       expr     min       lq     mean   median       uq      max neval
fun_1(df_2) 162.135 176.7315 197.8129 187.7065 201.0385 1555.802  1000
fun_2(df_2)  84.770  92.2840 102.3595  96.3135 108.8165 1441.410  1000
fun_3(df_2)  85.052  93.8675 103.7496  97.9310 109.4090 1422.860  1000

Solution

  • There is nothing rmongodb special here. As I wrote everywhere: rmongodb will convert unnamed lists into arrays and named lists into objects. So you just should to convert your second data.frame into correct list:

    df2_transformed <-  list('myArr' = Map(function(i, df) df[i, ], 
                                       seq_len(nrow(df_2)), 
                                       MoreArgs = list('df' = df_2)
                                      ))
    df1_df2_comb <- c(df_1, df2_transformed)
    str(df1_df2_comb)
    mongo.insert(mongo, paste0(db,".",coll), df1_df2_comb)
    

    You can use Map, lapply, mapply - depends on your preference.