Yassine Mrabet
Yassine Mrabet

Reputation: 23

R: sorting indiviual letters from separate columns and merge them in one string

I need a simple R function to sort, in alphabetical order, letters from col1:col3, by row, and concatenate them into one string, i.e. "a" "c" "b" and "c" "a" "b" will give the same string "abc", which will be stored in a new column. My input data.frame looks like this:

col1 col2 col3 val
a c b 3
e a a 2
c c b 1
c a b 6
... 

The result:

col1 col2 col3 val col4
a c b 3 abc
e a a 2 aae
c c b 1 bcc
c a b 6 abc
... 

Afterward, I need to sum up the rows yielding the same string, in order to obtain a new data.frame looking as below:

col1 val
abc 9
aae 5
bbc 12
...

Upvotes: 2

Views: 67

Answers (1)

nicola
nicola

Reputation: 24480

You can try:

df$col4<-apply(df[,1:3],1,function(x) paste(sort(x),collapse=""))
#  col1 col2 col3 val col4
#1    a    c    b   3  abc
#2    e    a    a   2  aae
#3    c    c    b   1  bcc
#4    c    a    b   6  abc

Then you use aggregate:

aggregate(val ~ col4,df,sum)
#  col4 val
#1  aae   2
#2  abc   9
#3  bcc   1

Upvotes: 1

Related Questions