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

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

$
0
0

I am trying to find the Euclidean distance between any two 0s in a list of data frames. 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)

Viewing all articles
Browse latest Browse all 206255

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>