Reputation: 1035
Let say I have the following data
AgentID Position FriendID
1 1 2
2 4 1
3 10 2
What I want to do is something like following
d[, .SD , by=AgentID] %>%
(for each of the subsets - disected according to AgentID, called the current AgentID -,
select the part of `d` where the FriendID is the current AgentID. )
(calculate the distance between Friend's position and current Agent's position)
How can I do that?
Do note that, the above is just a toy example to just to understand the concept
Edit:
The expected output would be like
AgentID Position FriendID distanceToFriend
1 1 2 3
2 4 1 3
3 10 2 6
Upvotes: 0
Views: 96
Reputation: 41220
The code below make a self join with data.table
from d
on itself, linking AgentID
with FriendID
via the on = .(AgentID = FriendID)
argument.
You might have a look at this tutorial to understand better the join logic.
In this kind of LHS[RHS,...]
join, if variables have the same name on both sides (which is obviously the case on a self join), you can choose which side to use with the x.
prefix (not used here) for LHS
and i.
prefix for RHS
.
x.
and i.
refers to general data.table
X[i,j,by]
syntax.
d[d,.(AgentID,
Position = i.Position,
FriendID = i.FriendID,
distance = abs(Position-i.Position))
,on = .(AgentID = FriendID)]
# AgentID Position FriendID distance
#1: 1 1 2 3
#2: 2 4 1 3
#3: 3 10 2 6
Upvotes: 3
Reputation: 388982
We can perform a self-join :
library(dplyr)
inner_join(df, df, by = c('AgentID' = 'FriendID')) %>%
transmute(FriendID = AgentID,
AgentID = AgentID.y,
distanceToFriend = abs(Position.x - Position.y)) %>%
arrange(AgentID)
# FriendID AgentID distanceToFriend
#1 2 1 3
#2 1 2 3
#3 2 3 6
Upvotes: 1
Reputation: 101337
Another data.table
option
d[
,
distanceToFriend := .SD[
,
abs(diff(d[, Position][unlist(.(AgentID, FriendID))]))
],
AgentID
][]
gives
AgentID Position FriendID distanceToFriend
1: 1 1 2 3
2: 2 4 1 3
3: 3 10 2 6
Upvotes: 0
Reputation: 887088
We can use
d$distanceToFriend <- abs(d$Position - d$Position[match(d$FriendID, d$AgentID)])
Upvotes: 0