简体   繁体   English

根据另一行中另一列的值将列添加到数据框

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

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. 我真的是R的初学者,所以我希望也许有人可以帮助我或对我有好的暗示。

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. 我试着在一个例子中描述我想做的事情:第一行是ID为125的人的数据。这个人的第一个伙伴是ID为715的人。我想创建一个新列,每个人的伴侣的答案的价值。 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). 所以R应该取列1.partnerID的值,在这种情况下是“715”并搜索该行,其中“715”是列ID中的值(没有多于一次的ID)。 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 ... 从这个特定的行R应该从列回答中获取值(在这个例子中是“5”)并将其放入新列“1.partneranswer”,但是在人125的行中。我希望有人可以明白我想做什么......

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). (特别是因为在我原来的data.frame中,每个人有5个合作伙伴,并且有多个列应该从中传输值,因此每个单独的每行写入它会花费很多时间)。

I hope someone can help. 我希望有人能帮帮忙。 Thank you! 谢谢!

One solution is to use apply as follows: 一种解决方案是使用如下apply

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 : 编辑:使用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

Update : This can be done with self join; 更新 :这可以通过自联接完成; The first two columns define a map relationship from ID to answer, in order to find the answers for the partner IDs, you can merge the data frame with itself with first data frame keyed on partnerID and the second data frame keyed on ID : 前两列定义了从ID到答案的映射关系,为了找到伙伴ID的答案,您可以将数据框与自身合并,其中第一个数据帧键入partnerID ,第二个数据帧键入ID

Suppose df is (fixed the column names a little bit): 假设df是(固定列名称一点点):

df
#   ID answer partnerID
#1 125      3       715
#2 235      4       845
#3 370      7       985
#4 560      1       950
#5 715      5       235
#6 950      5       560
#7 845      6       370
#8 985      6       125


merge(df, df[c('ID', 'answer')], by.x = "partnerID", by.y = "ID")

#  partnerID  ID answer.x answer.y
#1       125 985        6        3
#2       235 715        5        4
#3       370 845        6        7
#4       560 950        5        1
#5       715 125        3        5
#6       845 235        4        6
#7       950 560        1        5
#8       985 370        7        6

Old answer : If the ID and partnerID are mapped to each other one on one, you can try: 旧答案 :如果ID和partnerID相互映射,您可以尝试:

df$partneranswer <- with(df, answer[sapply(X1.partnerID, function(partnerID) which(ID == partnerID))])

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM