Renaming Rows to control items in dist() and promp() objects

99 views Asked by At

I have a data frame called family 1 (below). This data will be used for constructing dendrograms and for principal component analysis. I would like to control the names of items in both dist() and promp() objects, which use row names to indicate the relation between items (i.e., dissimilarity matrix). I have created an array of different dendrograms by systematically naming the rows (code below). However, my family names are are alpha numeric "X22", "X2", "X75", and "X87", and are different on every row, thus R seems to get confused by them. The data frame will be very large and systematically naming rows will be exhaustive. Each row in the data frame may be labelled and ordered with a different a family name (i.e., "X22", "X87", "X22", "X4", "X22".....,). In the below example I organised the family names using ddply(). The question I am asking is how to rename the rownames in this large dataset with their correct corresponding family name contained under the column heading "Family". Then I can extract the column "Family" from the data frame and just rely on the rows names to indicate dissimilarity relations between families for their values of SBI.x, when using dist() and promp().

#Dendrogram

library(dendextend)
family1$Family <- as.factor(family1$Family)
class(family1$Family)
str(family1)
family1

family2 <- ddply(family1,.(Family), summarise, SBI.CV.mean = mean(SBI.CV))
family2
class(family2)

rownames(family2)

[1] "1" "2" "3" "4"
rownames(family2)[1] <- "X22"
rownames(family2)[2] <- "X4"
rownames(family2)[3] <- "X75"
rownames(family2)[4] <- "X87"
rownames(family2)

[1] "X22" "X4"  "X75" "X87"

family2

keeps <- "SBI.CV.mean"
family3 <- family2[,keeps,drop=FALSE]
family3

 par(mfrow = c(3,3))
 require(graphics)
 dend.data <- hclust(dist(family3))
 dend <- as.dendrogram(dend.data)
 plot(dend)

 dend.data1 <- hclust(dist(family3), "single")
 dend1 <- as.dendrogram(dend.data1, horiz=T)
 plot(dend1, horiz=T)

 dend.data2 <- hclust(dist(family3), "ave")
 dend3 <- as.dendrogram(dend.data2, hang=0.02)
 plot(dend2, horiz=TRUE)

 dend.data3 <- hclust(dist(family3), "ave")
 dend3 <- as.dendrogram(dend.data3, hang=-1)
 plot(dend3)

 colour.dend1 <- as.dendrogram(dend3)
 colour.dend1
 dend1_mod_01 <- colour.dend1
 dend1_mod_01 <- colour_branches(dend1_mod_01, k=2)
 col_for_labels <- c("purple","purple","orange","purple","orange","dark      green")

dend_mod_01 <- color_labels(dend1_mod_01,col=col_for_labels)
plot(dend3)
plot(dend1_mod_01)


      Family     SBI.CV         SBI.x
  1      X22  66.666670  0.2859644782
  2      X22  50.000000 -0.2724271529
  3      X22  66.666670  0.2859644782
  4      X22  50.000000 -0.2724271529
  5      X22  66.666670  0.2859644782
  6      X22  50.000000 -0.2724271529
  7      X22  66.666670  0.2859644782
  8      X22  50.000000 -0.2724271529
  9      X22  66.666670  0.2859644782
  10      X4  50.000000 -0.2724271529
  11      X4  66.666670  0.2859644782
  12      X4  50.000000 -0.2724271529
  13      X4  66.666670  0.2859644782
  14      X4  50.000000 -0.2724271529
  15      X4  66.666670  0.2859644782
  16      X4  50.000000 -0.2724271529
  17      X4  66.666670  0.2859644782
  18      X4  50.000000 -0.2724271529
  19     X75 105.249338  1.5786185545
  20     X75  78.507843  0.6826851131
  21     X75  25.101428 -1.1066162398
  22     X75  67.395050  0.3103677511
  23     X75  67.857549  0.3258630822
  24     X75 145.284695  2.9199427839
  25     X75  60.806449  0.0896266157
  26     X75  71.595201  0.4510874730
  27     X75   8.845135 -1.6512588087
  28     X75 119.192646  2.0457680508
  29     X75  50.024029 -0.2716220975
  30     X75   7.733006 -1.6885190128
  31     X75  23.977506 -1.1442715506
  32     X75  37.055887 -0.7061001284
  33     X75  76.765617  0.6243144597
  34     X75  86.444781  0.9486002452
  35     X75  12.882740 -1.5159849452
  36     X75  74.266621  0.5405893693
  37     X75  45.465494 -0.4243489346
  38     X75  16.600339 -1.3914324000
  39     X75  42.897631 -0.5103813099
  40     X75  41.374846 -0.5613999237
  41     X75  73.292581  0.5079556288
  42     X75 134.677664  2.5645702145
  43     X75  13.588425 -1.4923420341
  44     X75  87.600497  0.9873207660
  45     X75  58.104290 -0.0009051445
  46     X75  43.961024 -0.4747539319
  47     X75  29.329649 -0.9649560749
  48     X75  25.727674 -1.0856348125
  49     X75  86.583227  0.9532386696
  50     X75  45.530280 -0.4221783774
  51     X75  37.480592 -0.6918710282
  52     X75  53.528554 -0.1542082751
  53     X75  52.208901 -0.1984212577
  54     X75  13.855732 -1.4833863164
  55     X75  73.891903  0.5280350081
  56     X75  50.819011 -0.2449874251
  57     X75  57.510157 -0.0208106742
  58     X75  39.984956 -0.6079660910
  59     X75  25.825865 -1.0823450712
  60     X75 100.217554  1.4100362237
  61     X75  47.522531 -0.3554310136
  62     X87   7.014259 -1.7125995466
  63     X87  70.808548  0.4247318511
  64     X87  29.447163 -0.9610189457
  65     X87  63.880988  0.1926344059
  66     X87  20.743852 -1.2526102488
  67     X87  85.819272  0.9276435100
  68     X87  85.635091  0.9214728035
  69     X87  45.392863 -0.4267823266
  70     X87  64.801549  0.2234764132
  71     X87  72.351927  0.4764404358
  72     X87 125.895232  2.2703280817
  73     X87  72.035520  0.4658396967
  74     X87  56.762990 -0.0458433772
  75     X87  54.722673 -0.1142011198
  76     X87 124.943928  2.2384560765
  77     X87  35.527844 -0.7572949035
  78     X87  47.422399 -0.3587857852
  79     X87  37.716625 -0.6839630986
  80     X87  68.930631  0.3618150755
  81     X87  74.545714  0.5499399592
  82     X87  50.891461 -0.2425600971
  83     X87   6.548454 -1.7282056403
  84     X87  94.367915  1.2140528952
  85     X87  42.406152 -0.5268475722
  86     X87  59.187538  0.0353874453
  87     X87 102.434045  1.4842964104
  88     X87  85.227130  0.9078046857
  89     X87 109.304906  1.7144942411
  90     X87  36.815668 -0.7141483035
  91     X87  18.201273 -1.3377955219
  92     X87  45.524410 -0.4223750429
  93     X87  99.347994  1.3809029280
  94     X87  66.913808  0.2942444640
  95     X87  79.170739  0.7048944434
  96     X87  75.674816  0.5877688181
  97     X87  65.629371  0.2512113403
  98     X87  16.569935 -1.3924510401
  99     X87  31.772731 -0.8831042987
  100    X87  38.017454 -0.6738842769
  101    X87  24.862990 -1.1146047452
  102    X87  34.972611 -0.7758971474
  103    X87 136.658358  2.6309303785
  104    X87  14.192113 -1.4721163785
0

There are 0 answers