1

将做一个简短的、可重复的示例来说明我遇到的问题,该示例涉及将数据从 R 插入到 mongo 数据库中。这很有挑战性,因为正如您将看到的,我有一个嵌套的数据列。解决这个问题对我的数据库至关重要,我认为其他人也可能遇到这个问题。

我的数据:

my.data <- structure(list(`_id` = c(10138L, 9466L, 9390L), firstName = c("Alex", "Quincy", "Steven"), lastName = c("Abrines", "Acy", "Adams"), 
    birthCity = c("Palma de Mallorca", "Tyler, TX", "Rotorua"
    ), birthCountry = c("Spain", "USA", "New Zealand")), row.names = c(NA, 
3L), class = "data.frame")

my.data
> nba_players
    _id firstName lastName         birthCity birthCountry
1 10138      Alex  Abrines Palma de Mallorca        Spain
2  9466    Quincy      Acy         Tyler, TX          USA
3  9390    Steven    Adams           Rotorua  New Zealand

inner.df <- structure(list(jerseyNumber = 40L, weight = 240L, age = 21L), class = "data.frame", row.names = 485L)

num.vector <- c(1,3,5,7)

我的上述目标是双重的:

  • inner.df向其中添加第 4 列num.vector
  • inner.df作为第 6 列添加到中的每一行my.data

...这是我用来执行此操作的代码:

# add a list of the numbers to inner df
inner.df$shotIDs = list(num.vector)  

# create allmonths column (name of the row where inner.df's will be placed)  
my.data <- my.data %>%
  dplyr::mutate(allmonths = NA)

# convert allmonths into a column of class == list
my.data$allmonths[1] = list(placeholder = NA)

# For EACH row in my main my.data dataframe, add the inner.df to the allmonths column/key
for(i in 1:nrow(my.data)) {
  my.data$allmonths[[i]] <- inner.df
}

# Write this to my mongo db
con <- mongolite::mongo(collection = 'mycoll', db = 'mydb', url = "myurl")
con$insert(my.data) # this is not a good way to update a db

这是我的结果(来自 Robo 3T):

在此处输入图像描述 ...

我对此非常接近,但由于某种原因,allmonths它是一个长度为 1 的数组,而不是它自己的对象。如果allmonths是一个具有 4 个字段的对象,其值与标记为 [0] 的对象完全相同,那么这会好得多。

有没有人看到我在这里的尝试有什么问题。我确信这是其他人在使用 R 中的嵌套对象时可能遇到的问题!任何帮助都非常感谢!

4

1 回答 1

2

要获取对象,{ }allmonths需要是类型的列data.frame,而不是list.

以你为例

library(dplyr)
    
my.data <- structure(list(`_id` = c(10138L, 9466L, 9390L), firstName = c("Alex", "Quincy", "Steven"), lastName = c("Abrines", "Acy", "Adams"), 
                              birthCity = c("Palma de Mallorca", "Tyler, TX", "Rotorua"
                              ), birthCountry = c("Spain", "USA", "New Zealand")), row.names = c(NA, 
                                                                                                 3L), class = "data.frame")
    
    
my.data
    
inner.df <- structure(list(jerseyNumber = 40L, weight = 240L, age = 21L), class = "data.frame", row.names = 485L)
    
num.vector <- c(1,3,5,7)
    
# add a list of the numbers to inner df
inner.df$shotIDs = list(num.vector)  

如果您现在将您的inner.df作为一列附加(必须重复它,因为您需要 3 行来匹配您的my.data

my.data$allmonths <- inner.df[rep(1,3), ]

然后查看它生成的 JSON,你会看到你得到了你的allmonths: { }对象

substr( jsonlite::toJSON( my.data ), 1, 196 )
# [{"_id":10138,"firstName":"Alex","lastName":"Abrines","birthCity":"Palma de Mallorca","birthCountry":"Spain",
# "allmonths":{"jerseyNumber":40,"weight":240,"age":21,"shotIDs":[1,3,5,7],"_row":"485"}
# } 


在旁边

构建您所追求的 JSON 通常很有帮助,然后调用fromJSON以查看您应该针对的 R 结构

js <- '
[{"_id":10138,"firstName":"Alex","lastName":"Abrines","birthCity":"Palma de Mallorca","birthCountry":"Spain","allmonths":{"jerseyNumber":40,"weight":240,"age":21,"shotIDs":[1,3,5,7],"_row":"485"}},{"_id":9466,"firstName":"Quincy","lastName":"Acy","birthCity":"Tyler, TX","birthCountry":"USA","allmonths":{"jerseyNumber":40,"weight":240,"age":21,"shotIDs":[1,3,5,7],"_row":"485.1"}},{"_id":9390,"firstName":"Steven","lastName":"Adams","birthCity":"Rotorua","birthCountry":"New Zealand","allmonths":{"jerseyNumber":40,"weight":240,"age":21,"shotIDs":[1,3,5,7],"_row":"485.2"}}] 
'
str( jsonlite::fromJSON( js ) )
    
# 'data.frame': 3 obs. of  6 variables:
#   $ _id         : int  10138 9466 9390
#   $ firstName   : chr  "Alex" "Quincy" "Steven"
#   $ lastName    : chr  "Abrines" "Acy" "Adams"
#   $ birthCity   : chr  "Palma de Mallorca" "Tyler, TX" "Rotorua"
#   $ birthCountry: chr  "Spain" "USA" "New Zealand"
#   $ allmonths   :'data.frame':    3 obs. of  4 variables:
#   ..$ jerseyNumber: int  40 40 40
#   ..$ weight      : int  240 240 240
#   ..$ age         : int  21 21 21
#   ..$ shotIDs     :List of 3
#   .. ..$ : int  1 3 5 7
#   .. ..$ : int  1 3 5 7
#   .. ..$ : int  1 3 5 7
于 2018-10-15T23:48:31.113 回答