Reputation: 11
I kind of have a noob question related to R. I have the following data table:
Receiver_id Sender_id Gender_receiver
1 2 Male
2 3 Female
3 4 Male
4 2 Female
5 3 Female
The receiver_id indicates the id_number of the person who received a message, whereas the inviter_id indicates the id_number of the person who send the message. In this case, person 2 sends something to person 1. The gender_receiver refers to the gender of the receiver. However, based on this it should be possible to also indicate the gender of the sender by creating a new column named Gender_sender. Unfortunately, I am not able to do so. I used the following code but without any success:
dt[, Gender_sender := dt$Gender_receiver, by=Sender_id]
I do not get the right allocation of gender.
Somebody knows what I should do, I think it is relatively simple but I cannot figure it out.
Thanks in advance, Amanda
Upvotes: 1
Views: 192
Reputation: 24074
You need to match the Gender with id of sender:
dt[, Gender_sender:=Gender_receiver[match(Sender_id, Receiver_id)]]
dt
# Receiver_id Sender_id Gender_receiver Gender_sender
#1: 1 2 Male Female
#2: 2 3 Female Male
#3: 3 4 Male Female
#4: 4 2 Female Female
#5: 5 3 Female Male
Upvotes: 6