0

I have 5 samples in the matrix below (mydf). The euclidean distance between centre to the samples is given by the Distance column. How can I form a relationship matrix between the pairwise combination of distance of samples (eg., A:A,A:B,A:C....E:E) intersecting the centre. So I need to calculate the euclidean distance from A to centre to A (A:A), then A to centre to B (A:B), and so on. For example, the distance from A to center to A is (0.03994220+0.03994220), then A to B is (0.03994220+0.03704120), and so on.

mydf

  samples  Distance        
    A        0.03994220        
    B        0.03704120
    C        0.03580851
    D        0.04404073
    E        0.04350807
MAPK
  • 5,635
  • 4
  • 37
  • 88

1 Answers1

1

Is this what you are after?

df1 <- as.data.frame(outer(df$Distance,df$Distance, '+'))
df1
#          V1         V2         V3         V4         V5
#1 0.07988440 0.07698340 0.07575071 0.08398293 0.08345027
#2 0.07698340 0.07408240 0.07284971 0.08108193 0.08054927
#3 0.07575071 0.07284971 0.07161702 0.07984924 0.07931658
#4 0.08398293 0.08108193 0.07984924 0.08808146 0.08754880
#5 0.08345027 0.08054927 0.07931658 0.08754880 0.08701614
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • 1
    look at `rownames` and `colnames` – Sotos Apr 15 '16 at 12:06
  • sorry this does not work with the dataframe I have: `df<-structure(list(V1 = structure(c(3L, 1L, 2L, 5L, 4L), .Names = c("860", "861", "862", "2529", "2530"), .Label = c("0.0321300642107672", "0.0330817724920314", "0.0339978194537914", "0.0413104564905709", "0.0415935059053326"), class = "factor")), .Names = "Distance", row.names = c("860", "861", "862", "2529", "2530"), class = "data.frame")` – MAPK Apr 15 '16 at 12:18
  • 1
    convert it to numeric first – Sotos Apr 15 '16 at 12:21
  • i.e. `df$Distance <- as.numeric(as.character(df$Distance))` – Sotos Apr 15 '16 at 12:23