Data reshaping - two columns of correlation variables

I have below df

    var1 var2 Freq
1    a    b   10
2    b    a    5
3    b    d   10

      

created from

help <- data.frame(var1 = c("a", "b", "b"), var2 = c("b", "a", "d"), Freq = c(10, 5, 10))

      

ab the correlation is the same as ba and I am hoping to concatenate them into one string to look like

   var1 var2 Freq
1    a    b   15
2    b    d   10

      

any thoughts?

+3


source to share


2 answers


Here's one way:



setNames(aggregate(help$Freq, as.data.frame(t(apply(help[-3], 1, sort))), sum), 
         names(help))

#   var1 var2 Freq
# 1    a    b   15
# 2    b    d   10

      

+2


source


In the R base:

do.call(rbind,
by(dat,rowSums(sapply(dat[,c("var1","var2")],as.integer)),
   function(x)data.frame(x[1,c("var1","var2")],
                         Freq= sum(x[,"Freq"]))))

  var1 var2 Freq
3    a    b   15
5    b    d   10

      



I am creating the first grouping variable by summing the integer representation of your columns. Then we perform the sum of frequencies by groups. Finally, bind the result to get a new data.frame.

0


source







All Articles