每個人。我有一個包含 1000 行(節點和鏈接)的資料集,在 txt 中有兩列 V1 和 V2,我使用 read.table 匯入了它們。資料集中有反轉的行,例如:
net <- read.table("DD242.txt", quote="\"", comment.char="")
V1 V2
4 5
5 4
6 7
7 8
and so on...
但我不知道哪些值重復。如何找到這些重復行并洗掉其中之一?在這種情況下,我想洗掉倒置的第二行 = 5 4。所以我只有:
V1 V2
4 5
6 7
7 8
非常感謝!
uj5u.com熱心網友回復:
data.table 解決方案
library(data.table)
setDT(net) # or use fread instead of read.table to get a data.table right away
net <- net[, sorted := apply(.SD, 1, function(x) list(unname(sort(x))))][!duplicated(sorted)]
net[, sorted := NULL]
結果
net
V1 V2
1: 4 5
2: 6 7
3: 7 8
資料
net <- data.frame(
V1 = c(4, 5, 6, 7),
V2 = c(5, 4, 7, 8)
)
uj5u.com熱心網友回復:
您可以filter通過lag:
library(dplyr)
df %>%
filter(!(V1 == lag(V2, default = 0) & V2 == lag(V1, default = 0)))
# V1 V2
#1 4 5
#2 6 7
#3 7 8
或者在基礎 R 中:
as.data.frame((df <- t(apply(df, 1, sort)))[!duplicated(df), ])
V1 V2
1 4 5
2 6 7
3 7 8
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/522682.html
標籤:r排序神经网络节点
