Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7969007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:10:06+00:00 2026-06-04T07:10:06+00:00

Given the matrix, df <- read.table(text=" X1 X2 X3 X4 X5 1 2 3

  • 0

Given the matrix,

df <- read.table(text="
 X1 X2 X3 X4 X5
  1  2  3  2  1
  2  3  4  4  3
  3  4  4  6  2
  4  5  5  5  4
  2  3  3  3  6
  5  6  2  8  4", header=T)

I want to create a distance matrix containing the absolute mean difference between each row of each column. For example, the distance between X1 and X3 should be = 1.67 given that:

abs(1 – 3) + abs(2-4) + abs(3-4) + abs(4-5) + abs(2-3) + abs(5-2) = 10 / 6 = 1.67

I have tried using the designdist() function in the vegan package this way:

designdist(t(df), method = "abs(A-B)/6", terms = "minimum")

The resulting distance for columns 1 and 3 is 0.666. The problem with this function is that it sums all the values in each column and then subtracts them. But I need to sum the absolute differences between each row (individually, absolute) and then divide it by N.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:10:07+00:00Added an answer on June 4, 2026 at 7:10 am

    Here’s a one-line solution. It takes advantage of dist()‘s method argument to calculate the L1 norm aka city block distance aka Manhattan distance between each pair of columns in your data.frame.

    as.matrix(dist(df, "manhattan", diag=TRUE, upper=TRUE)/nrow(df))
    

    To make it reproducible:

    df <- read.table(text="
     X1 X2 X3 X4 X5
      1  2  3  2  1
      2  3  4  4  3
      3  4  4  6  2
      4  5  5  5  4
      2  3  3  3  6
      5  6  2  8  4", header=T)
    
    dmat <- as.matrix(dist(df, "manhattan", diag=TRUE, upper=TRUE)/nrow(df))
    print(dmat, digits=3)
    #      1     2     3    4     5    6
    # 1 0.00 1.167 1.667 2.33 1.333 3.00
    # 2 1.17 0.000 0.833 1.17 0.833 2.17
    # 3 1.67 0.833 0.000 1.00 1.667 1.67
    # 4 2.33 1.167 1.000 0.00 1.667 1.33
    # 5 1.33 0.833 1.667 1.67 0.000 2.33
    # 6 3.00 2.167 1.667 1.33 2.333 0.00
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given graph, how could i represent it using adj matrix?. I have read lots
Given a matrix of size M and N , we want to fill in
I need to create a function to rotate a given matrix (list of lists)
given a matrix of distances between points is there an algorithm for determining a
Given the matrix structure(list(X1 = c(1L, 2L, 3L, 4L, 2L, 5L), X2 = c(2L,
Given the following Markov Matrix: import numpy, scipy.linalg A = numpy.array([[0.9, 0.1],[0.15, 0.85]]) The
Given an n-dimensional matrix of values: what is the most efficient way of retrieving
Given an arbitrary 4x4 transformation matrix, how do I find out the center of
Circumstances I have a procedure which will construct a matrix using the given list
Given the following table: id category_id categorizable_id categorizable_type 66 22 67 Image 72 22

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.