0

Say I have a vector of elements divided by group, I am looking for a way of getting the ordered selection of elements without repeatition by group. The example will probably clarify:

group<-c("a","a","a","b","b","b")
element<-c(1,2,3,1,2,3)
data<-data.frame(group,element)

I would like to get:

group<-c("a","a","a","b","b","b")
element.x<-(1,1,2,1,1,2)
element.y<-(2,3,3,2,3,3)
data<-data.frame(group, element.x,element.y)

I have tried to work with left_join from dplyr but it gives me all the combinations (i.e. (1,2) and (2,1) and it is not what I am looking for).

require(dplyr)
data<- data %>%
left_join(data , by=c("group"))

Possibly, I would prefer something that can work even if the variable "element" is a string. Thanks a lot.

A.Tulli
  • 23
  • 4

1 Answers1

0

You could try combn with tapply.

group <- c("a","a","a","b","b","b")
element <- c(1,2,3,1,2,3)
data <- data.frame(group,element)

# get per group, all unique combinations
combinations <- tapply(data$element, data$group, combn, 2)

# get number of unique combinations per group
nPerComb <- sapply(combinations, NCOL) 

# bind combinations to a matrix
elements <- t(do.call(cbind, combinations))

# fill dataframe with new information
data2 <- data.frame(
    group = rep(unique(data$group), nPerComb),
    element.x = elements[, 1],
    element.y = elements[, 2]
)

This also works for character data but not for factors (those will be converted to numeric).

Vandenman
  • 3,046
  • 20
  • 33