Home > Back-end >  How do I assign the value from one partner in dyad to the other partner in R (e.g. using dplyr)?
How do I assign the value from one partner in dyad to the other partner in R (e.g. using dplyr)?

Time:07-25

Suppose I have data on individuals within dyads that look like this:

    out <- data.frame("id" = seq(1:8), 
                      dyad =rep(letters[1:4], each = 2, 
                      times = 1), 
                      income = rep(c(1000, 900), 4)
                      )

Which gives me:

id dyad income
1 a 1000
2 a 900
3 b 1000
4 b 900
5 c 1000
6 c 900
7 d 1000
8 d 900

Suppose further that I want a column that gives me each partner's income within each dyad. The new column would look like this:

out$income_partner <- rep(c(900, 1000), 4)

So that the data look like this:

id dyad income income_partner
1 a 1000 900
2 a 900 1000
3 b 1000 900
4 b 900 1000
5 c 1000 900
6 c 900 1000
7 d 1000 900
8 d 900 1000

Can anyone help me to map responses from one member of each dyad to the other member of each dyad?

Grateful in advance for any advice.

CodePudding user response:

You could solve your problem as follow:

library(dplyr)

out %>%
  group_by(dyad) %>%
  mutate(income_partner = rev(income)) %>%
  ungroup()

# A tibble: 8 x 4
     id dyad  income income_partner
  <int> <chr>  <dbl>          <dbl>
1     1 a       1000            900
2     2 a        900           1000
3     3 b       1000            900
4     4 b        900           1000
5     5 c       1000            900
6     6 c        900           1000
7     7 d       1000            900
8     8 d        900           1000

CodePudding user response:

Another option using first and last:

out <- data.frame("id" = seq(1:8), 
                  dyad =rep(letters[1:4], each = 2, 
                            times = 1), 
                  income = rep(c(1000, 900), 4)
)

library(dplyr)
out %>%
  group_by(dyad) %>%
  mutate(income_partner = c(last(income), first(income))) %>%
  ungroup()
#> # A tibble: 8 × 4
#> # Groups:   dyad [4]
#>      id dyad  income income_partner
#>   <int> <chr>  <dbl>          <dbl>
#> 1     1 a       1000            900
#> 2     2 a        900           1000
#> 3     3 b       1000            900
#> 4     4 b        900           1000
#> 5     5 c       1000            900
#> 6     6 c        900           1000
#> 7     7 d       1000            900
#> 8     8 d        900           1000

Created on 2022-07-25 by the reprex package (v2.0.1)

CodePudding user response:

An alternative:

library(tidyverse)

out %>%
  group_by(dyad) %>%
  mutate(income_partner = income[3 - row_number()]) %>%
  ungroup()

# A tibble: 8 x 4
     id dyad  income income_partner
  <int> <chr>  <dbl>          <dbl>
1     1 a       1000            900
2     2 a        900           1000
3     3 b       1000            900
4     4 b        900           1000
5     5 c       1000            900
6     6 c        900           1000
7     7 d       1000            900
8     8 d        900           1000
  • Related