删除重复的2列排列 [英] Remove duplicated 2 columns permutations
问题描述
我找不到这个问题的标题,请随时编辑。
我有这个data.frame
部分时间从
1 a 9 1 2
2 a 9 2 1
3 a 12 2 3
4 a 12 2 4
5 a 12 3 2
6 a 12 3 4
7 a 12 4 2
8 a 12 4 3
我想将具有相同的重复行删除到
和从
同时,不计算2列的排列:例如(1,2)和(2,1)被重复。
所以最终输出将是:
部分时间从
1 a 9 1 2
3 a 12 2 3
4 a 12 2 4
6 a 12 3 4
我有一个解决方案,通过构造一个新的列键,例如
key< - paste(min(to,from),max to,from))
,并使用重复的
删除重复的密钥,但我认为这是脏的解决方案。 >
这里我的数据的输出
结构(list(section = c(1L,1L,1L,1L,1L,1L,
1L,1L),.Label =a,class =factor),time = c(9L,9L,12L,$ b $ (2L,1L,3L,4L,2L,2L,12L,12L,12L,12L),= c(1L,2L,2L,2L,3L,3L,4L,
4L) ,4L,2L,3L)),.Names = c(section,
time,to,from),row.names = c(NA,-8L),class = data.frame)
mn < - pmin(s $ to,s $ from)
mx< - pmax(s $ to,s $ from)
int< - as.numeric(interaction(mn ,mx))
s [match(unique(int),int)]]
从
开始的时间a b 1 a 9 1 2
3 a 12 2 3
4 a 12 2 4
6 a 12 3 4
问题:从数据框中删除连续的重复项,特别是@ MatthewPlourde的回答。
I can't find a good title for this question so feel free to edit it please.
I have this data.frame
section time to from
1 a 9 1 2
2 a 9 2 1
3 a 12 2 3
4 a 12 2 4
5 a 12 3 2
6 a 12 3 4
7 a 12 4 2
8 a 12 4 3
I want to remove duplicated rows that have the same to
and from
simultaneously, without computing permutations of the 2 columns: e.g (1,2) and (2,1) are duplicated.
So final output would be:
section time to from
1 a 9 1 2
3 a 12 2 3
4 a 12 2 4
6 a 12 3 4
I have a solution by constructing a new column key e.g
key <- paste(min(to,from),max(to,from))
and remove duplicated key using duplicated
, but I think this is dirty solution.
here the dput of my data
structure(list(section = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "a", class = "factor"), time = c(9L, 9L, 12L,
12L, 12L, 12L, 12L, 12L), to = c(1L, 2L, 2L, 2L, 3L, 3L, 4L,
4L), from = c(2L, 1L, 3L, 4L, 2L, 4L, 2L, 3L)), .Names = c("section",
"time", "to", "from"), row.names = c(NA, -8L), class = "data.frame")
mn <- pmin(s$to, s$from)
mx <- pmax(s$to, s$from)
int <- as.numeric(interaction(mn, mx))
s[match(unique(int), int),]
section time to from
1 a 9 1 2
3 a 12 2 3
4 a 12 2 4
6 a 12 3 4
Credit for the idea goes to this question: Remove consecutive duplicates from dataframe and specifically @MatthewPlourde's answer.
这篇关于删除重复的2列排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!