问题
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.
回答1:
Assuming neighboring areas are defined by areas within +/- 1 of the given area:
Copying from the previous question: Subsetting based on co-occurrence within a time window ...
with(dat,dat[
(
SP=="A" |
# Area %in% Area[SP=="A"]
Area %in% c(Area[SP=='A']-1, Area[SP=='A'], Area[SP=='A']+1)
) &
apply(
sapply(Time[SP=="A"],
function(x) abs(difftime(Time,x,units="mins"))<=30 ),1,any
)
,]
)
As per the comment (and my comment)
area <- 2
dat[dat$AR %in% c(area - 1, area, area + 1),]
And in Regards to Conditional subsetting by POSIXct interval and another field containing interval removing the conditional for SP=='A' should result in correct subsetting
area_boolean <- with(dat, Area %in% c(Area[SP=='A']-1, Area[SP=='A'], Area[SP=='A']+1))
time_boolean <- with(dat, apply(sapply(Time[SP=="A"],
function(x) abs(difftime(Time, x, units="mins")) <= 30 ),
1,
any))
dat[area_boolean & time_boolean,]
来源:https://stackoverflow.com/questions/18706182/subsetting-neighboring-fileds