代码之家  ›  专栏  ›  技术社区  ›  Cameron

从列中提取列表数据

r
  •  3
  • Cameron  · 技术社区  · 8 年前

    我有一个与此非常相似的数据框(基本上是一张城市和它们在地图上的坐标表)。注意,坐标是x,y值的列表。

    foo <- data.frame(
      city = c("chicago", "new york"), 
      coordinate = I(list(list(10, 15), list(20, 25))), 
      myabbr = c("chi", "ny")
    )
    
    bar <- subset(foo, select=c("city", "coordinate"))
    

    现在,我可以创建一个只包含城市和坐标的新表。我真的希望x和y值是一个单独的列。这就是我试过的

    bar <- subset(foo, select=c("city", "coordinate[1]", "coordinate[2]"))
    

    但我不知道该怎么做。

    7 回复  |  直到 8 年前
        1
  •  5
  •   markus    8 年前

    再给你一个选择,使用 listCol_w 从 splitstackshape 是的。

    library(splitstackshape)
    listCol_w(foo, "coordinate")
    #       city myabbr coordinate_fl_1 coordinate_fl_2
    #1:  chicago    chi              10              15
    #2: new york     ny              20              25
    
        2
  •  2
  •   Benjamin Schlegel    8 年前

    您可以使用 list[[index]] 是的。在您的情况下,您可以这样提取:

    foo <- data.frame(city=c("chicago", "new york"), coordinate=I(list(list(10, 15), list(20,25))), myabbr=c("chi", "ny"))
    foo$coordinate_x = foo$coordinate[[1]]
    foo$coordinate_y = foo$coordinate[[2]]
    foo
    
        3
  •  2
  •   Juergen    8 年前

    您需要的是从列表列“coordinate”中提取x和y元素。列表提取的完成方式如下 list[[index]] 在R。

    即

    foo <- data.frame(city=c("chicago", "new york"), coordinate=I(list(list(10, 15), list(20,25))), myabbr=c("chi", "ny"))
    
    bar <- subset(foo, select=c("city", "coordinate"))
    
    bar$x <- bar$coordinate[[1]]
    bar$y <- bar$coordinate[[2]]
    
    bar$coordinate <- NULL
    
        4
  •  2
  •   Sotos    8 年前

    这里的想法是 unlist 每一行坐标和 cbind 在城市里,也就是说。

    cbind(city = as.character(bar$city), 
          setNames(data.frame(apply(bar, 1, function(i)unlist(i$coordinate))), 
                   c('coordinate1', 'coordinate2')))
    

    这给了我们,

          city coordinate1 coordinate2
    1  chicago          10          20
    2 new york          15          25
    
        5
  •  2
  •   zx8754    8 年前

    我们可以取消列的列表并绑定回原始数据帧,请尝试:

    cbind(foo, do.call(rbind, lapply(foo$coordinate, unlist)))
    #       city coordinate myabbr  1  2
    # 1  chicago     10, 15    chi 10 15
    # 2 new york     20, 25     ny 20 25
    
        6
  •  1
  •   Onyambu    8 年前
    bar%>%
       group_by(city)%>%
       mutate(coordinate=list(unlist(coordinate)),
              n=list(paste0("coordinate",1:lengths(coordinate))))%>%
       unnest%>%
       spread(n,coordinate)
    
    # A tibble: 2 x 3
    # Groups:   city [2]
      city     coordinate1 coordinate2
      <fct>          <dbl>       <dbl>
    1 chicago          10.         15.
    2 new york         20.         25.
    
        7
  •  1
  •   Roman    8 年前

    你也可以尝尝三角面包

    library(tidyverse)
    
    foo %>% 
       mutate(coordinate=map(coordinate,~unlist(.) %>% 
                          paste(., collapse=","))) %>% 
       separate(coordinate, into = c("x", "y"), sep=",")
    # A tibble: 2 x 4
      city     x     y     myabbr
      <fct>    <chr> <chr> <fct> 
    1 chicago  10    15    chi   
    2 new york 20    25    ny  
    

    这给了你预期的结果

    .Last.value %>% 
    select(-myabbr)