Your data:
outputs <- list(structure(list(y = c("Instämmer helt", "Instämmer delvis",
"Instämmer knappast", "Instämmer inte alls"),
rfreq = c(29L, 40L, 6L, 2L)), class = "data.frame",
row.names = c(NA, -4L)),
structure(list(y = c("Instämmer helt", "Instämmer delvis",
"Instämmer knappast", "Instämmer inte alls"),
rfreq = c(32L, 38L, 8L, 2L)), class = "data.frame",
row.names = c(NA, -4L)))
Reduce
can be used for adding the columns, as @Allan Cameron said, and if combined with merge
, it can also be used to bind the rfreq
columns without repeating y
columns.
Reduce(function(df1,df2) merge(df1,df2, by = "y", suffixes = 1:2), outputs)
# y rfreq1 rfreq2
#1 Instämmer delvis 40 38
#2 Instämmer helt 29 32
#3 Instämmer inte alls 2 2
#4 Instämmer knappast 6 8
This approach can be applied to a list with more than two elements, but the column names are duplicated. The suffix 3,4,... are not automatically added to the resulted column names.
# Creating two more elements so now `outputs` has four elements
outputs[[3]] <- outputs[[1]]
outputs[[4]] <- outputs[[2]]
# Exactly same code
Reduce(function(df1,df2) merge(df1,df2, by = "y", suffixes = 1:2), outputs)
# The result:
# y rfreq1 rfreq2 rfreq1 rfreq2
#1 Instämmer delvis 40 38 40 38
#2 Instämmer helt 29 32 29 32
#3 Instämmer inte alls 2 2 2 2
#4 Instämmer knappast 6 8 6 8
#Warning message:
#In merge.data.frame(df1, df2, by = "y", suffixes = 1:2) :
# column names ‘rfreq1’, ‘rfreq2’ are duplicated in the result
Updates
As for why the row order get swapped in the resulted data frame, it is because merge
function by default sorts the merged rows lexicographically, as explained in its documentation:
The rows are by default lexicographically sorted on the common columns, but for sort = FALSE are in an unspecified order.
To avoid this default behavior, we can set sort = FALSE
Reduce(function(df1,df2) merge(df1,df2, by = "y", suffixes = 1:2, sort = FALSE), outputs)
# y rfreq1 rfreq2 rfreq1 rfreq2
#1 Instämmer helt 29 32 29 32
#2 Instämmer delvis 40 38 40 38
#3 Instämmer knappast 6 8 6 8
#4 Instämmer inte alls 2 2 2 2
#Warning message:
#In merge.data.frame(df1, df2, by = "y", suffixes = 1:2, sort = FALSE) :
# column names ‘rfreq1’, ‘rfreq2’ are duplicated in the result