/ 猿问

# R中考虑两列的唯一行，无顺序

2019-10-21 14:56:08

df<-cbind(c("a","b","c","b"),c("b","d","e","a"))

> df

[,1] [,2]

[1,] "a"  "b"

[2,] "b"  "d"

[3,] "c"  "e"

[4,] "b"  "a"

## 3 回答

www说

unique(t(apply(df, 1, sort)))

duplicated(t(apply(df, 1, sort)))

library(data.table)

unique(as.data.table(df)[, c("V1", "V2") := list(pmin(V1, V2),

pmax(V1, V2))], by = c("V1", "V2"))

#    V1 V2

# 1:  a  b

# 2:  b  d

# 3:  c  e

library(dplyr)

data.frame(df, stringsAsFactors = FALSE) %>%

mutate(key = paste0(pmin(X1, X2), pmax(X1, X2), sep = "")) %>%

distinct(key)

#   X1 X2 key

# 1  a  b  ab

# 2  b  d  bd

# 3  c  e  ce

library(dplyr)

df <- data.frame(v1 = c("a","b","c","b"), v2 = c("b","d","e","a"))

df\$key <- apply(df, 1, function(s) paste0(sort(s), collapse=''))

##   v1 v2 key

## 1  a  b  ab

## 2  b  d  bd

## 3  c  e  ce

## 4  b  a  ab

df %>% group_by(key) %>% do(head(., n = 1))

## Source: local data frame [3 x 3]

## Groups: key

##   v1 v2 key

## 1  a  b  ab

## 2  b  d  bd

## 3  c  e  ce

• 3 回答
• 0 关注
• 197 浏览

0/150