Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

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

$
0
0

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)

Viewing all articles
Browse latest Browse all 201839

Trending Articles