[R] Name of objects in a list

Marc marc.moragues at gmail.com
Thu Apr 24 16:24:22 CEST 2008


Hi useRs,

I am trying to compare several distance matrices obtained from subsets 
of variables from the same experiment. I put all the subsets in a list 
and then calculated the distance matrices with lapply. In order to do a 
mantel test between them I wrote a function the returns a list with the 
output of single tests. I would like to include the name of the tests, 
so I know what each element of the result list is. My code is as follows:

land <- list(s1536 = s1536, s384L = s384L, s96L = s96L, s384c = s384c, 
s96c = s96c, s384r = s384r, s96r = s96r)
library(analogue)
ldis <- lapply(land, function(x) as.dist(distance(x, method = "mixed")))
mantel.list <- function(x, ...) {
  require(ade4)
  for (i in 2:length(x)) {
    x1 <- x[[i - 1]]
    x2 <- x[[i]]
    y[[i]] <- mantel.rtest(x1, x2)
  }
  return(y)
}
lmantel <- mantel.list(ldis)

 > str(ldis)
List of 7
 $ s1536:Class 'dist'  atomic [1:15400] 0.357 0.260 0.341 0.342 0.267 ...
  .. ..- attr(*, "Labels")= chr [1:176] "CS301" "CS302" "CS303" "CS304" ...
  .. ..- attr(*, "Size")= int 176
  .. ..- attr(*, "call")= language as.dist.default(m = distance(x, 
method = "mixed"))
  .. ..- attr(*, "Diag")= logi FALSE
  .. ..- attr(*, "Upper")= logi FALSE
 $ s384L:Class 'dist'  atomic [1:15400] 0.497 0.243 0.453 0.440 0.277 ...
  .. ..- attr(*, "Labels")= chr [1:176] "CS301" "CS302" "CS303" "CS304" ...
  .. ..- attr(*, "Size")= int 176
  .. ..- attr(*, "call")= language as.dist.default(m = distance(x, 
method = "mixed"))
  .. ..- attr(*, "Diag")= logi FALSE
  .. ..- attr(*, "Upper")= logi FALSE

(...)

 > str(lmantel)
List of 7
 $ :List of 5
  ..$ sim   : num [1:99]  0.007734  0.002429 -0.021489 -0.000181  
0.021703 ...
  ..$ obs   : num 0.785
  ..$ rep   : int 99
  ..$ pvalue: num 0.01
  ..$ call  : language mantelnoneuclid(m1 = m1, m2 = m2, nrepet = nrepet)
  ..- attr(*, "class")= chr "rtest"
 $ :List of 5
  ..$ sim   : num [1:99]  0.00749  0.02850 -0.01301 -0.01376  0.00883 ...
  ..$ obs   : num 0.785
  ..$ rep   : int 99
  ..$ pvalue: num 0.01
  ..$ call  : language mantelnoneuclid(m1 = m1, m2 = m2, nrepet = nrepet)
  ..- attr(*, "class")= chr "rtest"
(...)

I would like to get something like

 > str(lmantel)
List of 7
 $ s1536_s384L
 :List of 5
  ..$ sim   : num [1:99]  0.007734  0.002429 -0.021489 -0.000181  
0.021703 ...
  ..$ obs   : num 0.785
  ..$ rep   : int 99
  ..$ pvalue: num 0.01
  ..$ call  : language mantelnoneuclid(m1 = m1, m2 = m2, nrepet = nrepet)
  ..- attr(*, "class")= chr "rtest"
(...)

Thanks for your help.

Marc.



More information about the R-help mailing list