Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add column to data frame based on values of another column in another row

Tags:

r

I was searching for an answer to my specific problem, but I didn't find a conclusion. I found this: Add column to Data Frame based on values of other columns , but it was'nt exactly what I need in my specific case. I'm really a beginner in R, so I hope maybe someone can help me or has a good hint for me.

Here an example of what my data frame looks like:

ID     answer  1.partnerID  
125    3       715        
235    4       845         
370    7       985          
560    1       950          
715    5       235          
950    5       560          
845    6       370          
985    6       125          

I try to describe what I want to do on an example: In the first row is the data of the person with the ID 125. The first partner of this person is the person with ID 715. I want to create a new column, with the value of the answer of each person´s partner in it. It should look like this:

ID     answer  1.partnerID  1.partneranswer    
125    3       715          5
235    4       845          6
370    7       985          6
560    1       950          5
715    5       235          4
950    5       560          1
845    6       370          7
985    6       125          3

So R should take the value of the column 1.partnerID, which is in this case "715" and search for the row, where "715" is the value in the column ID (there are no IDs more than once). From this specific row R should take the value from the column answer (in this example that´s the "5") and put it into the new column "1.partneranswer", but in the row from person 125. I hope someone can understand what I want to do ...

My problem is that I can imagine how to write this for each row per hand, but I think there need to be an easiear way to do it for all rows in once? (especially because in my original data.frame are 5 partners per person and there are more than one column from which the values should be transfered, so it would come to many hours work to write it for each single row per hand).

I hope someone can help. Thank you!

like image 457
esia_1 Avatar asked May 19 '16 14:05

esia_1


People also ask

How do you add a column to a DataFrame based on another column?

Using apply() method If you need to apply a method over an existing column in order to compute some values that will eventually be added as a new column in the existing DataFrame, then pandas. DataFrame. apply() method should do the trick.

How will you add the value of two columns in a pandas DataFrame to create another column?

Combine Two Columns Using + OperatorBy use + operator simply you can combine/merge two or multiple text/string columns in pandas DataFrame. Note that when you apply + operator on numeric columns it actually does addition instead of concatenation.


1 Answers

One solution is to use apply as follows:

df$partneranswer <- apply(df, 1, function(x) df$answer[df$ID == x[3]])

Output will be as desired above. There may be a loop-less approach.

EDIT: Adding a loop-less (vectorized answer) using match:

df$partneranswer <- df$answer[match(df$X1.partnerID, df$ID)]
df
   ID answer X1.partnerID partneranswer
1 125      3          715             5
2 235      4          845             6
3 370      7          985             6
4 560      1          950             5
5 715      5          235             4
6 950      5          560             1
7 845      6          370             7
8 985      6          125             3
like image 186
Gopala Avatar answered Sep 24 '22 00:09

Gopala