[R] Matrix problem to extract animal associations

ilai keren at math.montana.edu
Mon Feb 27 17:04:06 CET 2012


set.seed(1)
(DFid <- data.frame(
x = sample(1:20,10),
y = sample(1:20,10),
IDs = sapply(1:10,function(i) paste("ID",i,sep=""))))

require(spdep)
coordinates(DFid) <- ~x+y
coords <- coordinates(DFid)
dnn4 <- dnearneigh(DFid,0,4)
 summary(dnn4)
 plot(DFid)
 plot(dnn4,coords,add=T,col=2)
 nb2mat(dnn4, zero.policy=TRUE)

This just one option from the multitude of spatial packages.

HTH


On Sun, Feb 26, 2012 at 4:55 PM, Ross Dwyer <ross.dwyer at uq.edu.au> wrote:
> Dear List,
>
> I have been trying to extract associations from a matrix whereby individual locations are within a certain distance threshold from one another.
>
> I have been able to extract those individuals where there is 'no interaction' (i.e. where these individuals are not within a specified distance threshold from another individual) and give these individuals a unique Group ID containing that one individual.
>
> i.e.
>
>   ID Group
>
> 1 ID1     1
>
> 2 ID3     2
>
> 3 ID4     3
>
> 4 ID5     4
>
> 5 ID7     5
>
> 6 ID8     6
>
> 7 ID9     7
>
>
> What I need assistance with is allocating associations with a unique group id.
> i.e. If we have interactions between  "ID2_ID6", "ID6_ID2", "ID6_ID10", "ID10_ID6" as in the example code...
>
>
>   ID Group
>
> 1 ID1     1
>
> 2 ID3     2
>
> 3 ID4     3
>
> 4 ID5     4
>
> 5 ID7     5
>
> 6 ID8     6
>
> 7 ID9     7
>
> ##
> 8 ID2     8
> 9 ID6     8
> 10 ID10     8
>
> ##
> The code also needs to robust enough to recognize instances where we have an interaction in a separate group...
> i.e. "ID11_ID12" should be in a separate group (Group 9) as they don't interact with IDs 2, 6, or 10 (not in below code!)
> 11 ID11     9
> 12 ID12     9
>
>
> I've been trying to figure this out but have drawn a blank. My example code can be found below.
>
> Very best wishes,
>
> Ross
>
> Dr Ross Dwyer
> Postdoctoral Research Fellow
> University of Queensland
>
>
>
>> ###
>> require(stats)
>> x <- sample(1:20,10)
>> y <- sample(1:20,10)
>> IDs <- sapply(1:10,function(i) paste("ID",i,sep=""))
>> (DFid <- data.frame(x,y))
>    x  y
> 1   7 20
> 2   5  3
> 3  12  5
> 4   3 12
> 5  18 19
> 6   2  1
> 7  19 15
> 8  20 11
> 9  13 14
> 10  1  2
>>
>>
>> (DMdist <- dist(DFid, method = "euclidean",
> +                diag = FALSE, upper = TRUE))
>           1         2         3         4         5         6         7         8         9        10
> 1            17.117243 15.811388  8.944272 11.045361 19.646883 13.000000 15.811388  8.485281 18.973666
> 2  17.117243            7.280110  9.219544 20.615528  3.605551 18.439089 17.000000 13.601471  4.123106
> 3  15.811388  7.280110           11.401754 15.231546 10.770330 12.206556 10.000000  9.055385 11.401754
> 4   8.944272  9.219544 11.401754           16.552945 11.045361 16.278821 17.029386 10.198039 10.198039
> 5  11.045361 20.615528 15.231546 16.552945           24.083189  4.123106  8.246211  7.071068 24.041631
> 6  19.646883  3.605551 10.770330 11.045361 24.083189           22.022716 20.591260 17.029386  1.414214
> 7  13.000000 18.439089 12.206556 16.278821  4.123106 22.022716            4.123106  6.082763 22.203603
> 8  15.811388 17.000000 10.000000 17.029386  8.246211 20.591260  4.123106            7.615773 21.023796
> 9   8.485281 13.601471  9.055385 10.198039  7.071068 17.029386  6.082763  7.615773           16.970563
> 10 18.973666  4.123106 11.401754 10.198039 24.041631  1.414214 22.203603 21.023796 16.970563
>>
>> #Generate True/False matrix on those individuals < 4 units apart
>> DMTF <- apply(as.matrix(DMdist), c(1,2), function(x) ifelse(x<=4,T,F))
>> diag(DMTF)<- NA #replace diagonal with NA
>> dimnames(DMTF) <- list(IDs, IDs) #add individual's name to matrix
>>
>> DMTF
>       ID1   ID2   ID3   ID4   ID5   ID6   ID7   ID8   ID9  ID10
> ID1     NA FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> ID2  FALSE    NA FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
> ID3  FALSE FALSE    NA FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> ID4  FALSE FALSE FALSE    NA FALSE FALSE FALSE FALSE FALSE FALSE
> ID5  FALSE FALSE FALSE FALSE    NA FALSE FALSE FALSE FALSE FALSE
> ID6  FALSE  TRUE FALSE FALSE FALSE    NA FALSE FALSE FALSE  TRUE
> ID7  FALSE FALSE FALSE FALSE FALSE FALSE    NA FALSE FALSE FALSE
> ID8  FALSE FALSE FALSE FALSE FALSE FALSE FALSE    NA FALSE FALSE
> ID9  FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE    NA FALSE
> ID10 FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE    NA
>>
>> irow <- as.character(gl(length(IDs),length(IDs),labels=IDs))
>> icol <-rep(IDs, length(IDs))
>>
>> AssocMatrix <- matrix(data=paste(irow,"_",icol,"_",sep=""),
> +                       nrow = length(IDs), ncol = length(IDs),
> +                       dimnames= list(IDs, IDs))
>>
>> AssocMatrix
>
>     ID1         ID2         ID3         ID4         ID5         ID6         ID7         ID8         ID9         ID10
>
> ID1  "ID1_ID1_"  "ID2_ID1_"  "ID3_ID1_"  "ID4_ID1_"  "ID5_ID1_"  "ID6_ID1_"  "ID7_ID1_"  "ID8_ID1_"  "ID9_ID1_"  "ID10_ID1_"
>
> ID2  "ID1_ID2_"  "ID2_ID2_"  "ID3_ID2_"  "ID4_ID2_"  "ID5_ID2_"  "ID6_ID2_"  "ID7_ID2_"  "ID8_ID2_"  "ID9_ID2_"  "ID10_ID2_"
>
> ID3  "ID1_ID3_"  "ID2_ID3_"  "ID3_ID3_"  "ID4_ID3_"  "ID5_ID3_"  "ID6_ID3_"  "ID7_ID3_"  "ID8_ID3_"  "ID9_ID3_"  "ID10_ID3_"
>
> ID4  "ID1_ID4_"  "ID2_ID4_"  "ID3_ID4_"  "ID4_ID4_"  "ID5_ID4_"  "ID6_ID4_"  "ID7_ID4_"  "ID8_ID4_"  "ID9_ID4_"  "ID10_ID4_"
>
> ID5  "ID1_ID5_"  "ID2_ID5_"  "ID3_ID5_"  "ID4_ID5_"  "ID5_ID5_"  "ID6_ID5_"  "ID7_ID5_"  "ID8_ID5_"  "ID9_ID5_"  "ID10_ID5_"
>
> ID6  "ID1_ID6_"  "ID2_ID6_"  "ID3_ID6_"  "ID4_ID6_"  "ID5_ID6_"  "ID6_ID6_"  "ID7_ID6_"  "ID8_ID6_"  "ID9_ID6_"  "ID10_ID6_"
>
> ID7  "ID1_ID7_"  "ID2_ID7_"  "ID3_ID7_"  "ID4_ID7_"  "ID5_ID7_"  "ID6_ID7_"  "ID7_ID7_"  "ID8_ID7_"  "ID9_ID7_"  "ID10_ID7_"
>
> ID8  "ID1_ID8_"  "ID2_ID8_"  "ID3_ID8_"  "ID4_ID8_"  "ID5_ID8_"  "ID6_ID8_"  "ID7_ID8_"  "ID8_ID8_"  "ID9_ID8_"  "ID10_ID8_"
>
> ID9  "ID1_ID9_"  "ID2_ID9_"  "ID3_ID9_"  "ID4_ID9_"  "ID5_ID9_"  "ID6_ID9_"  "ID7_ID9_"  "ID8_ID9_"  "ID9_ID9_"  "ID10_ID9_"
>
> ID10 "ID1_ID10_" "ID2_ID10_" "ID3_ID10_" "ID4_ID10_" "ID5_ID10_" "ID6_ID10_" "ID7_ID10_" "ID8_ID10_" "ID9_ID10_" "ID10_ID10_"
>>
>> (AMatrix <- as.character(unique(AssocMatrix[which(DMTF==TRUE)])))
> [1] "ID2_ID6"  "ID6_ID2"  "ID6_ID10" "ID10_ID6"
>>
>
>> ##Extract those individuals not in any interactions
>
>> nonassoc <- IDs[is.na(charmatch(inassoc,AMatrix))]
>
>>
>
>> (NGroups <- data.frame(ID=nonassoc,Group=1:length(nonassoc)))
>
>   ID Group
>
> 1 ID1     1
>
> 2 ID3     2
>
> 3 ID4     3
>
> 4 ID5     4
>
> 5 ID7     5
>
> 6 ID8     6
>
> 7 ID9     7
>
>
>
>>
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list