【问题标题】:List of lists by unique coordinates按唯一坐标列出的列表
【发布时间】:2017-12-24 06:58:25
【问题描述】:

我有一个如下所示的数据框。不过,我想折叠它,以便每个唯一坐标都是其 SubID 的列表。

       subID                  latlon
1  S20298920 29.2178694, -94.9342990
2  S35629295 26.7063982, -80.7168961
3  S35844314 26.7063982, -80.7168961
4  S35833936 26.6836236, -80.3512144
7  S30634757 42.4585456, -76.5146989
8  S35834082 26.4330582, -80.9416786
9  S35857972 26.4330582, -80.9416786
10 S35833885 26.7063982, -80.7168961

所以,在这里,我希望 (26.7063982, -80.7168961) 成为包含 (S35629295, S35844314) 的列表,并且 (29.2178694, -94.9342990) 成为仅包含 (S20298920) 的列表。我认为列表是最有意义的。

【问题讨论】:

  • latlon 列的类型是什么,或者那里真的有两列?
  • 要么,要么,实际上。现在它是一个单列,latlon,属于character,但我也有(未显示)相同的信息分成两列,latlon。谢谢。
  • tidyr::nest(df, subID) 也许,尽管从技术上讲,这会产生数据框的列表列。仅使用向量,使用 dplyr,df %>% group_by(latlon) %>% summarise_all(list) 或在基础中,aggregate(subID ~ latlon, df, list)

标签: r list


【解决方案1】:

使用aggregate:

out <- aggregate(data=df,subID~latlon,FUN = function(t) list(sort(paste(t))))

由于您的数据集庞大且繁琐,下面的示例代码使用了淡化的数据,更易于阅读。

out <- aggregate(data=df,name~ID,FUN = function(t) list(sort(paste(t))))
out
  ID          name
1  1 apple, orange
2  2        orange
3  3 apple, orange

数据:

df <- data.frame(ID=c(1,1,2,3,3),
                 name=c('apple', 'orange', 'orange', 'orange', 'apple'))

Demo

【讨论】:

    【解决方案2】:
       with(data,tapply(subID,latlon,as.list))
    

    输出:

    $`26.4330582 -80.9416786`
    $`26.4330582 -80.9416786`[[1]]
    [1] "S35834082"
    
    $`26.4330582 -80.9416786`[[2]]
    [1] "S35857972"
    
    
    $`26.6836236 -80.3512144`
    $`26.6836236 -80.3512144`[[1]]
    [1] "S35833936"
       :
       :
       :
    

    数据:

     data=read.table(text="subID latlon
     S20298920 '29.2178694 -94.9342990'
     S35629295 '26.7063982 -80.7168961'
     S35844314 '26.7063982 -80.7168961'
     S35833936 '26.6836236 -80.3512144'
     S30634757 '42.4585456 -76.5146989'
     S35834082 '26.4330582 -80.9416786'
     S35857972 '26.4330582 -80.9416786'
     S35833885 '26.7063982 -80.7168961' ",h=T,stringsAsFactors=F)
    

    【讨论】:

      【解决方案3】:

      在tidyverse中,你可以使用tidyr::nest,它会嵌套数据框:

      library(tidyverse)
      
      df <- data_frame(subID = c("S20298920", "S35629295", "S35844314", "S35833936", "S30634757", "S35834082", "S35857972", "S35833885"), 
                       latlon = c("29.2178694, -94.934299", "26.7063982, -80.7168961", "26.7063982, -80.7168961", "26.6836236, -80.3512144", "42.4585456, -76.5146989", "26.4330582, -80.9416786", "26.4330582, -80.9416786", "26.7063982, -80.7168961"))
      
      df %>% nest(subID)
      #> # A tibble: 5 x 2
      #>                    latlon             data
      #>                     <chr>           <list>
      #> 1  29.2178694, -94.934299 <tibble [1 x 1]>
      #> 2 26.7063982, -80.7168961 <tibble [3 x 1]>
      #> 3 26.6836236, -80.3512144 <tibble [1 x 1]>
      #> 4 42.4585456, -76.5146989 <tibble [1 x 1]>
      #> 5 26.4330582, -80.9416786 <tibble [2 x 1]>
      

      或者只是用list 总结来制作一个向量列表列:

      df %>% 
          group_by(latlon) %>% 
          summarise_all(list)
      #> # A tibble: 5 x 2
      #>                    latlon     subID
      #>                     <chr>    <list>
      #> 1 26.4330582, -80.9416786 <chr [2]>
      #> 2 26.6836236, -80.3512144 <chr [1]>
      #> 3 26.7063982, -80.7168961 <chr [3]>
      #> 4  29.2178694, -94.934299 <chr [1]>
      #> 5 42.4585456, -76.5146989 <chr [1]>
      

      【讨论】:

        猜你喜欢
        • 2018-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多