Home > Mobile >  Merging multiple wide-format data.frames into a single long-format data.frame in R
Merging multiple wide-format data.frames into a single long-format data.frame in R

Time:12-22

I was wondering if there is a way to merge wide-format data.frames (time1,time2,time1and2) into a single long-format data.frame to achieve my Desired_output below?

time1 =read.table(text="

class id   order ac bc
1     1    s-c   1  2

",h=TRUE)

time2 =read.table(text="

class id   order ac bc
1     1    s-c   3  4

",h=TRUE)


time1and2 =read.table(text="

class id   order ex1S ex2S ex1C ex2C
1     1    s-c   8    5    6     1

",h=TRUE)
Desired_output="
class id   order time DV score ave_ex
1     1    s-c   1    ac 1     (8 5)/2 =6.5  # ave_ex = average of `ex`
1     1    s-c   1    bc 2     (8 5)/2 =6.5
1     1    s-c   2    ac 3     (6 1)/2 =3.5
1     1    s-c   2    bc 4     (6 1)/2 =3.5
"

CodePudding user response:

One dplyr/tidyr option could be to bind the first two tables, pivot them longer, then join them with the third table and mutate:

library(dplyr)
library(tidyr)

rbind(cbind(time1, time = 1), cbind(time2, time = 2)) %>%
  pivot_longer(ac:bc,names_to = "DV", values_to = "score") %>%
  right_join(time1and2) %>%
  mutate(ave = case_when(
    time == 1 ~ mean(c(ex1S, ex2S)),
    time == 2 ~ mean(c(ex1C, ex2C))
  )) %>%
  select(-c(ex1S:ex2C))

Output

#   class    id order  time DV    score   ave
#   <int> <int> <chr> <dbl> <chr> <int> <dbl>
# 1     1     1 s-c       1 ac        1   6.5
# 2     1     1 s-c       1 bc        2   6.5
# 3     1     1 s-c       2 ac        3   3.5
# 4     1     1 s-c       2 bc        4   3.5

  • Related