ags*_*udy 5 r duplicates dataframe
我找不到适合该问题的标题,请随时对其进行编辑。
我有这个数据
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
Run Code Online (Sandbox Code Playgroud)
我想删除具有相同to且from同时的重复行,而不计算2列的排列:例如(1,2)和(2,1)是重复的。
因此最终输出将是:
section time to from
1 a 9 1 2
3 a 12 2 3
4 a 12 2 4
6 a 12 3 4
Run Code Online (Sandbox Code Playgroud)
我有一个通过构造一个新的列键的解决方案,例如
key <- paste(min(to,from),max(to,from))
Run Code Online (Sandbox Code Playgroud)
并使用删除重复的密钥duplicated,但是我认为这是肮脏的解决方案。
这是我的数据
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")
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
这个想法的功劳在于这个问题: 从数据框中删除连续的重复项,特别是@MatthewPlourde的答案。
| 归档时间: |
|
| 查看次数: |
1181 次 |
| 最近记录: |