Akira
Akira

Reputation: 2870

How to remove duplicate vectors from a list?

I have a list of vector list_of_vector. If elements of my list were numbers, it would be easy. Two vectors a, b in my list are the same if they are of the same length and a[i] = b[i] for all i. Could you please elaborate on this issue?

stop_times_data = read.table(file = "https://raw.githubusercontent.com/leanhdung1994/Statistical_Consulting/main/stop_times.txt",
                             sep = ",", header = TRUE)
stop_times_data <- stop_times_data[c("stop_id", "stop_sequence")]

ind <- (stop_times_data$stop_sequence == 1)
stop_times_data$start_journey <- as.numeric(ind)
n <- nrow(stop_times_data)
se <- seq(from = 1, to = n, by = 1)
row <- se[ind]
m <- length(row)
stop_times_data$between_stations <- 0

for (i in 1:(m - 1)){
  stop_times_data[row[i], c("between_stations")] <- row[i + 1] - row[i]
}
stop_times_data[row[m], c("between_stations")] <- n - row[m] + 1

## Generate a list of vectors

list_of_vector <- replicate(m, data.frame())

for (i in 1:length(row)) {
  df <- stop_times_data[row[i]:(row[i] + stop_times_data[row[i], "between_stations"] - 1), 1]
  list_of_vector[[i]] <- df
}

list_of_vector[[1]]

Upvotes: 1

Views: 165

Answers (1)

jay.sf
jay.sf

Reputation: 72633

We can apply negated duplicated also on lists.

list_of_vector[!duplicated(list_of_vector)]
# [[1]]
# [1] 4000519 4000521 4000523 4000525 4000527 4000529
# [7] 4000531 4000533 4000535 4000537 4000539 4000541
# [13] 4000542 4000544 4000545 4000546 4000548 4000550
# [19] 4000552 4000554 4000556 4000558 4000560 4000562
# [25] 4000564 4000566 4000568
# 
# [[2]]
# [1] 4000541 4000542 4000544 4000545 4000546 4000548
# [7] 4000550 4000552 4000554 4000556 4000558
# 
# [[3]]
# [1] 4000569 4000567 4000565 4000563 4000561 4000559
# [7] 4000557 4000555 4000553 4000551 4000549 4000547
# [13] 4000570 4000571 4000572 4000543 4000540 4000538
# [19] 4000536 4000534 4000532 4000530 4000528 4000526
# [25] 4000524 4000522 4000520
# 
# [[4]]
# [1] 4000559 4000557 4000555 4000553 4000551 4000549
# [7] 4000547 4000570 4000571 4000572 4000543
# 
# [[5]]
# [1] 4000543 4000540 4000538 4000536 4000534 4000532
# [7] 4000530 4000528 4000526 4000524 4000522 4000520
# 
# [[6]]
# [1] 4000559 4000557 4000555 4000553 4000551 4000549
# [7] 4000547 4000570 4000571 4000572 4000543 4000540
# [13] 4000538 4000536 4000534 4000532 4000530 4000528
# [19] 4000526 4000524 4000522 4000520

Upvotes: 1

Related Questions