简体   繁体   中英

How would I output the euclidean distance between any two 1s in a list of binary data frames?

I am trying to find the Euclidean distance between any two 0s in multiple data frames in a list. Firstly, I have outputted the positions of each 0 for each data frame in the list...

csvs <- list.files(pattern="*.csv")
csv <- lapply(csv, read.table)
pos_0 <- sapply(csv, function(x) which(t(x) == 1))

But I am unsure what the next step would be. I have tried using the dist package but I don't know whether this is right...

dist(position_of_1s, method = "euclidean", diag = TRUE, upper = TRUE, p = 2)

I get the error

Error in dist(position_of_1s, method = "euclidean", diag = TRUE, upper = TRUE, : (list) object cannot be coerced to type 'double'

Is it possible to do this?

I have also provided a reproducible example below:

set.seed(99)
mat_list1 <- matrix(sample(c(0,1), 400, prob=c(0.8,0.2), replace=TRUE), nrow = 20)
df1 <- data.frame(mat_list1)

set.seed(123)
mat_list2 <- matrix(sample(c(0,1), 400, prob=c(0.8,0.2), replace=TRUE), nrow = 20)
df2 <- data.frame(mat_list2)

df_list <- list(df1, df2)

position_of_1s <- sapply(df_list, function(x) which(t(x) == 1))
dist(position_of_1s, method = "euclidean", diag = TRUE, upper = TRUE, p = 2)

Edited: I have now got something working however, could anyone please tell me if this would be correct or not?

set.seed(99)
mat_list1 <- matrix(sample(c(0,1), 400, prob=c(0.8,0.2), replace=TRUE), nrow = 20)
df_1 <- data.frame(mat_list1)

set.seed(123)
mat_list_2 <- matrix(sample(c(0,1), 400, prob=c(0.8,0.2), replace=TRUE), nrow = 20)
df_2 <- data.frame(mat_list_2)

df_list <- list(df_1, df_2)

position_1s <- sapply(df_list, function(x) which(x != 0, arr.ind = T))

dist_func <- function(x) {
  dist(x, method = "euclidean", diag = TRUE, upper = TRUE, p = 2)
}
lapply(position_1s, dist_func)

This appears to work though I'm not sure why. I just mirrored one of the examples from Distance Matrix Computation Documentation . I hope it helps!

dist(rbind(position_of_1s[[1]], position_of_1s[[2]]), method = "euclidean", diag = TRUE, upper = TRUE, p = 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM