将成对组合的名称附加到列表

时间:2016-03-07 19:57:23

标签: r

我有3个列表,表明在给定时间内2只动物之间的接近程度。这是通过使用成对方法(combn)产生的。是否可以采用实际的动物ID(例如A4116)并将其附加到列表中?

这是接近输出。

list(structure(list(date = structure(c(1433653200, 1433682060, 
1433682300, 1433682600, 1433682900, 1433683200, 1433683500, 1433683800, 
1433684100, 1433684400, 1433684700, 1433685000, 1433685300, 1433685900, 
1433686200, 1433686500, 1433686800, 1433687100, 1433687400, 1433687700, 
1433688000, 1433688300, 1433688900, 1433689200, 1433689500, 1433689800, 
1433690100, 1433690400, 1433690700, 1433691000, 1433691300, 1433691600, 
1433691900, 1433692200, 1433692500, 1433692800, 1433693100, 1433693400, 
1433693700, 1433694300, 1433694600, 1433694900, 1433695200, 1433695500, 
1433695800, 1433696100, 1433696400, 1433710860, 1433714400, 1433714700, 
1433715000, 1433715300, 1433715600, 1433715900, 1433716200, 1433716500, 
1433716800, 1433717100, 1433717400, 1433717700, 1433718000, 1433718300, 
1433718600, 1433718900, 1433719200, 1433719500, 1433719800, 1433720100, 
1433720400, 1433720700, 1433721000, 1433721300, 1433721600, 1433721900, 
1433722200, 1433722500, 1433722800, 1433723100, 1433723400, 1433723700, 
1433724300, 1433724600, 1433724900, 1433725200), tzone = "", class =     c("POSIXct", 
"POSIXt")), prox = c(9852.45631332424, 9256.33691160873, 9028.68901891875, 
9191.98454236344, 9419.21684290166, 9671.30255336172, 9830.75308759428, 
9561.0623066864, 9754.12626540247, 9914.8333326201, 10032.8147462789, 
10134.1911746532, 10256.3686316126, 10299.6386637114, 10264.813853394, 
10226.513483445, 10200.9914435967, 10114.6211002648, 10033.1627759789, 
10027.3996075443, 10178.9657798564, 10306.1419110169, 10387.1972555159, 
10379.3391636744, 10380.5326690487, 10377.2955281855, 10381.4443573461, 
10383.1560136697, 10420.836268638, 10426.8905028026, 10502.7200747277, 
10535.2575462163, 10594.3223250264, 10597.3862632973, 10598.399237251, 
10682.447720954, 10762.1740930328, 10839.2692545212, 10844.2745836363, 
10931.9700306265, 10991.9810134246, 11018.2000809708, 11030.5881203101, 
11033.1756919616, 11039.5956196323, 11052.7357579988, 11061.479822531, 
11440.8641768448, 11190.6982378553, 11212.8610972399, 11265.7727951786, 
11279.3139675975, 11281.3435987417, 11295.2128870084, 11333.6039350659, 
11344.6839765253, 11343.2713562699, 11381.3498540076, 11381.8983043719, 
11379.7801019193, 11377.8452087527, 11377.5389562582, 11377.7994813318, 
11377.7513085892, 11410.7523306918, 11413.8663400647, 11412.9187800428, 
11390.3601843617, 11123.3701781985, 11117.097264913, 11201.6160216199, 
11172.4893668244, 11238.8818225423, 11243.4402711286, 11288.7133457441, 
11339.184630926, 11434.8515563023, 11457.8771419946, 11546.8583351167, 
11614.6942899304, 11748.1305387018, 11861.4880297284, 11876.4591252796, 
11889.7695580693)), .Names = c("date", "prox"), row.names = c(NA, 
-84L), class = "data.frame"), structure(list(date = structure(c(1433682060, 
1433686800, 1433689800, 1433693100, 1433694000, 1433716800, 1433719500, 
1433722500, 1433723700, 1433724060), tzone = "", class = c("POSIXct", 
"POSIXt")), prox = c(549.074862805507, 104.169908524786, 6.20587494958462, 
14.4090163841548, 11.1893094877397, 6.48113118292342, 4.2590421797149, 
10.6482102234669, 4.18129714439814, 7.57456599446394)), .Names = c("date", 
"prox"), row.names = c(NA, -10L), class = "data.frame"), structure(list(
date = structure(c(1433667660, 1433682060, 1433685660, 1433686800, 
1433688660, 1433689800, 1433693100, 1433716800, 1433719500, 
1433722500, 1433723700), tzone = "", class = c("POSIXct", 
"POSIXt")), prox = c(9737.13020663772, 8779.64324286579, 
10098.7523050675, 10183.7023830398, 10389.1848241986, 10371.4700328155, 
10751.7325538567, 11339.3375719111, 11415.9325121014, 11348.6761596755, 
11610.6754882626)), .Names = c("date", "prox"), row.names = c(NA, 
-11L), class = "data.frame"))

这是成对的动物名单。

structure(c(1L, 2L, 1L, 3L, 2L, 3L), .Dim = 2:3, .Label = c("A4116F", 
"A4117M", "A4118F"), class = "factor")

我正在试图弄清楚如何使用动物的实际ID附加3个列表(第一个列表将有一个带有“A4116F-A4117M”的新ID列,第二个列表将具有带有“A4116F”的ID列-A4118F“,依此类推。我有很多人,如果可能的话,我希望避免手动插入。

非常感谢!

2 个答案:

答案 0 :(得分:0)

如果prox是您的邻近表列表,ids是您的名称对表:

lapply(1:length(prox), function(i) {
  data.frame(prox[[i]], animal1 = ids[1, i], animal2 = ids[2, i])
})

这是有效的,因为data.frame()在创建新列时会重复比现有列更短的向量。

编辑:我假设您的列表顺序与您的名称对表的列顺序相匹配。

列表元素3的输出:

# [[3]]
#                   date      prox animal1 animal2
# 1  2015-06-07 05:01:00  9737.130  A4117M  A4118F
# 2  2015-06-07 09:01:00  8779.643  A4117M  A4118F
# 3  2015-06-07 10:01:00 10098.752  A4117M  A4118F
# 4  2015-06-07 10:20:00 10183.702  A4117M  A4118F
# 5  2015-06-07 10:51:00 10389.185  A4117M  A4118F
# 6  2015-06-07 11:10:00 10371.470  A4117M  A4118F
# 7  2015-06-07 12:05:00 10751.733  A4117M  A4118F
# 8  2015-06-07 18:40:00 11339.338  A4117M  A4118F
# 9  2015-06-07 19:25:00 11415.933  A4117M  A4118F
# 10 2015-06-07 20:15:00 11348.676  A4117M  A4118F
# 11 2015-06-07 20:35:00 11610.675  A4117M  A4118F

答案 1 :(得分:0)

这是一个解决方案:

col.tags <- apply(matrix(as.character(your.tags),2,3) ,2,paste0,collapse="-")

res <- lapply(seq_along(your.list), function(x){your.list[[x]]$ids<-col.tags[x] ;your.list[[x]] })

其中your.tagsyour.list是您提供的两种结构