b222
b222

Reputation: 976

Reshaping dataframe - two columns from correlation variables

I have the 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 correlation is the same as ba, and I am hoping to combine them into one row to look like

   var1 var2 Freq
1    a    b   15
2    b    d   10

any thoughts?

Upvotes: 3

Views: 82

Answers (2)

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

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

Upvotes: 2

agstudy
agstudy

Reputation: 121568

In base R :

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 create first a grouping variable by summing the integer representation of your columns. Then performing the sum of frequencies by group. Finally bind the result to get a new data.frame.

Upvotes: 0

Related Questions