3

I have a dataframe containing several rows with pairs in this format

41,25
25,41
23,41
41,23

I only want pairs that are distinct and the order doesn't matter. So for example the dataframe should look like this instead

41,25
41,25
41,23
41,23

So I can count how many times the combination 41 and 25 occures.

So I want to run through the whole dataframe and sort each string so the highest values comes first.(or lowest, doesn't matter).

Hope for you help. I'm guessing it's pretty simple, but can't figure it out.

Thanks

1
  • I assume you have a character column with this data then? Commented May 22, 2013 at 15:01

2 Answers 2

3

If d is your data.frame and the pairs are strings in one column, then

d[, 1] <- sapply(strsplit(d[, 1], ','), function(x) paste(sort(x), collapse=','))

If the pairs are split between two columns, say columns 1 and 2, then

d[, 1:2] <- t(apply(apply(d[, 1:2], 1, identity), 2, sort))
Sign up to request clarification or add additional context in comments.

Comments

0

This might be embarrassing when seen against the slick coding of @Matthew Plourde, but I don't think you wanted to remove duplicate rows, so here is the solution I came up with:

#Make some data
set.seed(1)
N <- 200
MIN <- 1
MAX <- 8
df <- data.frame(x=paste(round(runif(N, min=MIN, max=MAX)), round(runif(N, min=MIN, max=MAX)), sep=","))

#Split data into 2 components
require(plyr)
tmp <- ldply(strsplit(as.character(df$x), ","))
df$x1 <- as.numeric(tmp[,1])
df$x2 <- as.numeric(tmp[,2])
head(df)

#sort smaller of values to the left
tmp <- t(apply(df[,2:3], 1, sort))
df$x.sort <- apply(tmp, 1, paste, collapse=",")
head(df)

#sort df
ord <- order(df$x.sort)
df <- df[ord,]
head(df, 20)

Which results in:

      x x1 x2 x.sort
167 1,1  1  1    1,1
92  1,2  1  2    1,2
27  1,3  1  3    1,3
71  3,1  3  1    1,3
28  4,1  4  1    1,4
47  1,4  1  4    1,4
55  1,4  1  4    1,4
67  4,1  4  1    1,4
81  4,1  4  1    1,4
116 1,4  1  4    1,4
133 1,4  1  4    1,4
152 5,1  5  1    1,5
10  1,6  1  6    1,6
85  6,1  6  1    1,6
99  7,1  7  1    1,7
109 7,1  7  1    1,7
5   2,2  2  2    2,2
12  2,2  2  2    2,2
22  2,2  2  2    2,2
56  2,2  2  2    2,2

1 Comment

Thanks! Worked like a charm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.