通过唯一坐标列出的列表 [英] List of lists by unique coordinates

查看:77
本文介绍了通过唯一坐标列出的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的数据框.不过,我想折叠它,以便每个唯一坐标都是其SubID的列表.

I have a data frame like the one below. I want to collapse it, though, so that each unique coordinate is a list of its SubIDs.

       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)的列表.我认为列表列表才是最有意义的.

So, here, I want (26.7063982, -80.7168961) to be a list containing (S35629295, S35844314), and (29.2178694, -94.9342990) to be a list containing just (S20298920). I think a list of lists is what makes most sense.

推荐答案

使用aggregate:

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

由于您的数据集庞大且繁琐,因此下面的示例代码使用了简化后的数据,易于阅读.

Since your data set is large and cumbersome, the sample code below uses watered down data which is easier to read.

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'))

演示

这篇关于通过唯一坐标列出的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆