0

I have a simple data frame as the following:

    v1 <- c(1,2)
    v2 <- c(1,3)
    v3 <- c(2,1)
    df.test <- data.frame(rbind(v1,v2,v3))
    colnames(df.test) <- c('from', 'to')

I want to use a for loop to find & append all rows into a separate vector and then use unique or some set function to get the unique vector pair and store them into a new data frame. For now, I have:

    c <- c()

    for (row in 1:nrow(df.test)) {
        from <- df.test[row, "from"]
        to  <- df.test[row, "to"]
        new.row <- c(from, to)
        c <- c(c, new.row)

        # having trouble storing unique values here...


     }

Ideally, after taking only the unique value pairs, the new dataframe should look like this:enter image description here

Can anyone please help? Thank you!

Edward Lin
  • 609
  • 1
  • 9
  • 16
  • If you sort every pair before rbind, you can simply use unique() around the data frame to remove duplicated pairs, e.g. unique(rbind(sort(v1),sort(v2),sort(v3))) – caw5cv Feb 25 '18 at 19:30
  • thanks, but imagine there are hundreds of rows, then apply `sort()` to each of them will be somehow cumbersome – Edward Lin Feb 25 '18 at 19:34

1 Answers1

1

Try this:

# sort rows
df.test <- t(apply(df.test, 1, function(x) sort(x, decreasing=F)))

# get unique rows
df.test <- unique(df.test)
print(df.test)
YOLO
  • 20,181
  • 5
  • 20
  • 40