Elizabeth - 1 year ago 69

R Question

I have a dataframe with distances

`df<-data.frame(site.x=c("A","A","A","B","B","C"),`

site.y=c("B","C","D","C","D","D"),Distance=c(67,57,64,60,67,60))

I need to convert this to an object of class "dist" but I do not need to calculate a distance so therefore I cannon use the dist() function. Any advice?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

I had a similar problem not to long ago and solved it like this:

```
n <- max(table(df$site.x)) + 1 # +1, so we have diagonal of
res <- lapply(with(df, split(Distance, df$site.x)), function(x) c(rep(NA, n - length(x)), x))
res <- do.call("rbind", res)
res <- rbind(res, rep(NA, n))
res <- as.dist(t(res))
```