从r数据帧中提取数据

问题描述 投票:-1回答:1

我在从R中的数据框(exhibitionPlannings_1)中提取数据时遇到问题。

数据框如下所示:

> str(exhibitionPlannings_1)
'data.frame':   3 obs. of  4 variables:
 $ date               : chr  "23.08.2018" "24.08.2018" "27.08.2018"
 $ exhibitionLocations:List of 3
  ..$ :'data.frame':    2 obs. of  17 variables:
  .. ..$ id                                  : chr  "ind-8cf04a9734132302f96da8e113e80ce5-0" "5"
  .. ..$ title                               : chr  "title1" "title2"
  .. ..$ street                              : chr  "street1" "street2"
  .. ..$ $$hashKey                           : chr  "object:57" "object:287"
  .. ..$ exhibitorID                         :List of 2
  .. .. ..$ : list()
  .. .. ..$ : list()
  .. ..$ zip                                 : chr  "" "32584"
  .. ..$ city                                : chr  "" "city2"
  .. ..$ latitude                            : chr  "" "12.345"
  .. ..$ exhibitors                          :List of 2
  .. .. ..$ : list()
  .. .. ..$ : list()
  .. ..$ distance2Neighbour                  : chr  "123 km" ""
  .. ..$ duration2Neighbour                  : chr  "0 Stunden, 0 Minuten" ""
  .. ..$ forum                               : logi  NA TRUE
  .. ..$ longitude                           : num  NA 1.23
  .. ..$ class                               : chr  NA ""
  .. ..$ distance2LastLocationInPlanning     : chr  NA "00 km"
  .. ..$ distanceValue2LastLocationInPlanning: int  NA 123456
  .. ..$ duration2LastLocationInPlanning     : chr  NA "0 Stunden, 0 Minuten"
  ..$ :'data.frame':    1 obs. of  17 variables:
  .. ..$ id                                  : int 3
  .. ..$ forum                               : logi FALSE
  .. ..$ exhibitorID                         :List of 1
  .. .. ..$ : list()
  .. ..$ title                               : chr "title3"
  .. ..$ street                              : chr "street3"
  .. ..$ zip                                 : chr "123456"
  .. ..$ city                                : chr "city3"
  .. ..$ latitude                            : num 12.3
  .. ..$ longitude                           : num 1.23
  .. ..$ class                               : chr ""
  .. ..$ exhibitors                          :List of 1
  .. .. ..$ : list()
  .. ..$ $$hashKey                           : chr "object:310"
  .. ..$ distance2LastLocationInPlanning     : chr "0 km"
  .. ..$ distanceValue2LastLocationInPlanning: int 123456
  .. ..$ duration2LastLocationInPlanning     : chr "0 Stunden, 0 Minuten"
  .. ..$ distance2Neighbour                  : chr ""
  .. ..$ duration2Neighbour                  : chr ""
  ..$ :'data.frame':    2 obs. of  17 variables:
  .. ..$ id                                  : chr  "ind-4dbe13088312d7841a318997b1426dd0-3" "5"
  .. ..$ title                               : chr  "title4" "title5"
  .. ..$ street                              : chr  "street4" "street5"
  .. ..$ $$hashKey                           : chr  "object:60" "object:287"
  .. ..$ exhibitorID                         :List of 2
  .. .. ..$ : int 
  .. .. ..$ : int  1 10 25 37 38 56 74
  .. ..$ zip                                 : chr  "" "123456"
  .. ..$ city                                : chr  "" "city5"
  .. ..$ latitude                            : chr  "" "12.345"
  .. ..$ exhibitors                          :List of 2
  .. .. ..$ :'data.frame':  0 obs. of  0 variables
  .. .. ..$ :'data.frame':  1 obs. of  8 variables:
  .. .. .. ..$ id          : int 1
  .. .. .. ..$ title       : chr "ABC"
  .. .. .. ..$ contentUri  : chr "uri.com"
  .. .. .. ..$ locationName:List of 1
  .. .. .. .. ..$ : chr "location Name"
  .. .. .. ..$ locationID  :List of 1
  .. .. .. .. ..$ : int 5
  .. .. .. ..$ image       : chr "www.image.com"
  .. .. .. ..$ class       : chr "favorite"
  .. .. .. ..$ $$hashKey   : chr "object:65"
  .. ..$ distance2LastLocationInPlanning     : chr  "12,3 km" "456 km"
  .. ..$ distanceValue2LastLocationInPlanning: int  12345 6789
  .. ..$ duration2LastLocationInPlanning     : chr  "00 Minuten" "0 Stunden, 00 Minuten"
  .. ..$ distance2Neighbour                  : chr  "0 km" ""
  .. ..$ duration2Neighbour                  : chr  "0 Stunden, 00 Minuten" ""
  .. ..$ forum                               : logi  NA TRUE
  .. ..$ longitude                           : num  NA 1.23
  .. ..$ class                               : chr  NA ""
 $ $$hashKey          : chr  "object:298" "object:438" "object:562"
 $ googleStaticMap    : chr  "https://maps.googleapis.com/maps/api/staticmap?123"| __truncated__

正如您所见,“日期”有3个日期,“展览地点”是3个元素的列表,其中每个元素都是数据框。

我想为每个数据框添加一个新列。此列应包含相应的日期。

结果应如下所示:

date | id | title | street 
23.08.2018 | "ind-8cf04a9734132302f96da8e113e80ce5-0" | "title1" | "street1"
23.08.2018 | "5" | "title2" | "street2"
24.08.2018 | 3 | "title3" | "street3"
27.08.2018 | "ind-4dbe13088312d7841a318997b1426dd0-3" | "title4" | "street4"
27.08.2018 | "5" | "title5" | "street5"  

如果你能用map()建议解决方案,我会很高兴。

提前谢谢了!

r
1个回答
0
投票

你可以使用mapply将函数应用于两个参数(charachter vector和data.frame list),然后使用do.call对mapply的结果进行rbind:

# data.frame simulation

df <- structure(list(date = c("23.08.2018", "24.08.2018", "27.08.2018"
), dfs = structure(list(structure(list(id = structure(2:1, .Label = c("5", 
"ind-8cf04a9734132302f96da8e113e80ce5-0"), class = "factor"), 
    title = structure(1:2, .Label = c("title1", "title2"), class = "factor"), 
    street = structure(1:2, .Label = c("street1", "street2"), class = "factor")), class = "data.frame", row.names = c(NA, 
-2L)), structure(list(id = structure(1L, .Label = "3", class = "factor"), 
    title = structure(1L, .Label = "title3", class = "factor"), 
    street = structure(1L, .Label = "street3", class = "factor")), class = "data.frame", row.names = c(NA, 
-1L)), structure(list(id = structure(2:1, .Label = c("5", "ind-4dbe13088312d7841a318997b1426dd0-3"
), class = "factor"), title = structure(1:2, .Label = c("title4", 
"title5"), class = "factor"), street = structure(1:2, .Label = c("street4", 
"street5"), class = "factor")), class = "data.frame", row.names = c(NA, 
-2L))), class = "AsIs")), class = "data.frame", row.names = c(NA, 
-3L))

# extract and concatenate the data    
dfs <- mapply(function(x, y) {
    y$date <- x
    y
  } , df$date, df$dfs, SIMPLIFY = FALSE)

do.call(rbind, dfs)

输出:

                                                 id  title  street       date
23.08.2018.1 ind-8cf04a9734132302f96da8e113e80ce5-0 title1 street1 23.08.2018
23.08.2018.2                                      5 title2 street2 23.08.2018
24.08.2018                                        3 title3 street3 24.08.2018
27.08.2018.1 ind-4dbe13088312d7841a318997b1426dd0-3 title4 street4 27.08.2018
27.08.2018.2                                      5 title5 street5 27.08.2018
© www.soinside.com 2019 - 2024. All rights reserved.