2
votes

The question sounds a bit generic but I think an example would be much clearer:

I have the following two data frames

data1

group1     group2       group3     Level 
cat         cat          dog        1
dog         parrot       cat        1
mouse       dolphin      dolphin    1
red         blue         blue       2
green       yellow       green      2
black       purple       cat        2

data2

var1        level    Score
cat           1        1
dog           1        1
mouse         1        1
dolphin       1        0
parrot        1        1
red           2        1
blue          2        1
green         2        1
purple        2        1
cat           2        0
black         2        0
yellow        2        1

I want to modify data1 incorporating 3 new columns (one for each group1, group2 and group3) with the values I find in the column "score" from data2 depending on the levels of "level" (level is a factor). So basically I want to obtain something like this:

group1     group2       group3     Level      var1     var2     var3
cat         cat          dog        1          1        1        1
dog         parrot       cat        1          1        1        1
mouse       dolphin      dolphin    1          1        0        0
red         blue         blue       2          1        1        1
green       yellow       green      2          1        1        1
black       purple       cat        2          0        1        0

Sample Data

df1 <- structure(list(
  group1 = c("cat", "dog", "mouse", "red", "green", "black"),
  group2 = c("cat", "parrot", "dolphin", "blue", "yellow", "purple"),
  group3 = c("dog", "cat", "dolphin", "blue", "green", "cat"),
  Level = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("1", "2"), class = "factor")),
  row.names = c(NA, -6L), class = "data.frame")

df2 <- structure(list(
  var1 = c("cat", "dog", "mouse", "dolphin", "parrot", "red", "blue", "green", "purple", "cat", "black", "yellow"),
  level = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", "2"), class = "factor"),
  Score = c(1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L)),
  row.names = c(NA, -12L), class = "data.frame")
2
@akrun: good point, I change it - Carbo
@Darren Tsai: they should, I unintentionally write them differently - Carbo
@Darren Tsai : another mistake, I included yellow in data2: basically data 2 is created from data1 + the variable Score that is manually done: the output shows the intention to get back to table 1 format matching the variable score to each group - Carbo

2 Answers

3
votes

We can pivot the first dataset into 'long' format, join with the second one and then pivot it back to 'wide' format

library(dplyr)
library(tidyr)
library(stringr)
df1 %>%
    mutate(rn = row_number()) %>%
    pivot_longer(cols  = -c(rn, Level), values_to = 'var1') %>% 
    rename(level = Level) %>% 
    left_join(df2) %>% 
    mutate(name = str_replace(name, 'group', 'varn')) %>% 
    na.omit %>%
    select(-level, -var1) %>% 
    pivot_wider(names_from = name, values_from = Score, values_fill = list(Score = 0)) %>% 
    select(-rn) %>% 
    bind_cols(df1, .)
#   group1  group2  group3 Level varn1 varn2 varn3
#1    cat     cat     dog     1     1     1     1
#2    dog  parrot     cat     1     1     0     1
#3  mouse dolphin dolphin     1     1     0     0
#4    red    blue    blue     2     1     1     1
#5  green  yellow   green     2     1     0     1
#6  black  purple     cat     2     0     1     0
2
votes

I merge df2 to df1 three times recursively by purrr::reduce(). In this part, I copy df2 three times and change their first column names to match those in df1 respectively.

library(tidyverse)

df2 %>%
  list %>% rep(3) %>%
  imap(~ setNames(.x, c(str_c("group", .y), "Level", str_c("Score", .y)))) %>%
  reduce(left_join, .init = df1)

#   group1  group2  group3 Level Score1 Score2 Score3
# 1    cat     cat     dog     1      1      1      1
# 2    dog  parrot     cat     1      1      1      1
# 3  mouse dolphin dolphin     1      1      0      0
# 4    red    blue    blue     2      1      1      1
# 5  green  yellow   green     2      1      1      1
# 6  black  purple     cat     2      0      1      0