-1

I've three data-frames that I would like to join. They don't have the same length and how to join two of them was answered here. I was trying to use join_all from plyr but no dress for success as it takes the 'x' ranging from 2-7 from the df.a and by-passes the x values for 1 from df.b. I struggle to find the answer in the documentation for join_all.

MWE:

library(plyr)
df.a <- c(5, 4, 5, 7, 3, 5, 6, 5, 5, 4, 5, 5, 4, 5, 4, 7, 2, 4, 4, 5, 3, 6, 5, 6, 4, 4, 5, 4, 5, 5, 6, 7, 4)
df.b <- c(1, 3, 4, 6, 2, 7, 7, 4, 3, 6, 6, 3, 6, 6, 5, 6, 6, 5)
df.c <- c(3, 1, 3, 6, 6, 5, 7, 6, 6, 2, 7, 5, 1)
table(df.a)
count(df.a)
df.a.count <- count(df.a)
df.b.count <- count(df.b)
df.c.count <- count(df.c)

#normalize the data
df.a.count$freq <- sapply(df.a.count$freq, function(X) X/length(df.a))
df.b.count$freq <- sapply(df.b.count$freq, function(X) X/length(df.b))
df.c.count$freq <- sapply(df.c.count$freq, function(X) X/length(df.c))

#solution using merge
df.m <- merge(df.a.count, df.b.count, by ='x', all=TRUE)
df.m <- merge(df.m, df.c.count, by ='x', all=TRUE)[2:4]
names(df.m) <- c('freq.a', 'freq.b','freq.c')

#problem using join_all
dfs <- list(df.a.count, df.b.count, df.c.count) 
df.all <- join_all(dfs, 'x')
Community
  • 1
  • 1
raumkundschafter
  • 429
  • 1
  • 8
  • 24
  • Use `Reduce(function(...) merge(..., by = "x", all = TRUE), dfs)` – akrun Nov 25 '16 at 13:06
  • 1
    As far as I can see the marked duplicate doesn't indicate how this could be achieved using `join_all`. As a conclusion it is not suitable for this particular operation? – raumkundschafter Nov 25 '16 at 13:23
  • Regarding the problem with `join_all`, i am not sure if it is a bug or not. – akrun Nov 25 '16 at 14:43

1 Answers1

0

We can use Reduce with merge on the list of data.frames

Reduce(function(...) merge(..., by = "x", all = TRUE), dfs)
#   x     freq.x     freq.y       freq
#1 1         NA 0.05555556 0.15384615
#2 2 0.03030303 0.05555556 0.07692308
#3 3 0.06060606 0.16666667 0.15384615
#4 4 0.30303030 0.11111111         NA
#5 5 0.39393939 0.11111111 0.15384615
#6 6 0.12121212 0.38888889 0.30769231
#7 7 0.09090909 0.11111111 0.15384615
akrun
  • 874,273
  • 37
  • 540
  • 662