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')