I am trying to make a conditional subsetting which would include contemporary elements inside window of neighboring areas. For example, given the matrix Dat, where Species (SP), Area (AR) and Time (TM):
SP AR TM
A 2 2
B 2 2
C 1 4
F 3 2
B 5 3
E 3 2
D 2 1
I 1 4
H 3 2
E 2 4
D 3 5
B 1 2
How can I retrieve all the species co-occurring with species A in the same time, but within neighboring areas (in this case 1 and 3)? The desired output being:
SP AR TM
A 2 2
B 2 2
F 3 2
H 3 2
B 1 2
This is based on the assumption that Species A will be occurring repeatedly in the dataset in different areas. I have an attempt, given by user thelatemail from a different question (with related elements) I posted previously, with minor modifications. The X's added, indicate the part of the syntax I cannot figure out, which would basically be the definition of the bracket (given this is more or less where it should be going).
with(dat,dat[
apply(
sapply(TM[SP=="A"],
function(x) abs(AR)XXXXX),1,any
)
,]
)
Any help is much appreciated.
This is part of a set of operations I am trying to make on a large dataset. I broke the question into two elements, which are somewhat related but far from being duplicates. The reason for this being I am a beginning R user and want to learn how to interpret, write and integrate codes on my own. The link to the related question: Subsetting based on co-occurrence within a time window . I can remove one of the links if needed.
Ris well covered in many tutorials on line.dat[dat$TM==2,]gives your desired result... however more specifically:dat[dat$TM==2 & dat$AR %in% 1:3,]where that1:3can be a separate function to define "neighboring" areas. - Justin