user4400727
user4400727

Reputation:

Obtaining elements within elements from a list in R

I want to extract elements embedded within elements from a list using the lapply function. The original list contains elements that happen to be arrays and the gpagen function from the R package geomorph spits out a new list where each element contains an element embedded within it and I want to take these embedded elements and direct them to a new object.

Input example:

>unaligned_coords
$individual1
, , rep1

         [,1]      [,2]     [,3]
[1,] 58.81923  2.452332 273.2005
[2,] 68.24838 -4.298481 271.2884
[3,] 69.58006 -1.127088 270.0316

, , rep2

         [,1]      [,2]     [,3]
[1,] 58.82360  2.485687 273.1929
[2,] 68.14501 -4.233420 271.3564
[3,] 69.02335 -0.875766 270.4671

$individual2
, , rep1

         [,1]       [,2]     [,3]
[1,] 58.64723 -1.7827113 271.4866
[2,] 69.53938 -7.0807052 274.4347
[3,] 72.35068  0.1114307 272.1112

, , rep2

         [,1]       [,2]     [,3]
[1,] 58.63091 -1.7735939 271.4576
[2,] 68.78160 -6.9430161 274.1252
[3,] 73.07706  0.3313497 272.1727

...

Using lapply on the input example:

aligned_coords <- lapply(unaligned_coords, gpagen)

Output, where elements (coords) are embedded within elements (individuals):

>aligned_coords$individual1$coords
, , rep1

           [,1]         [,2]         [,3]
[1,] -0.1838745 -0.002361134  0.001343360
[2,]  0.1717784 -0.065992358 -0.003245113
[3,]  0.1701612  0.025533125 -0.068783434

, , rep2

           [,1]        [,2]          [,3]
[1,] -0.1844239  0.00147890  0.0001282347
[2,]  0.1846240 -0.07485290 -0.0031490047
[3,]  0.1681969  0.02331805 -0.0627923402

>aligned_coords$individual2$coords
, , rep1

           [,1]         [,2]        [,3]
[1,] -0.1439503  0.007217439  0.04066458
[2,]  0.1154014 -0.009312094 -0.10422833
[3,]  0.1673553  0.117909836  0.03056590

, , rep2

            [,1]         [,2]        [,3]
[1,] -0.14248417  0.004972831  0.03475940
[2,]  0.09739824 -0.008022416 -0.09583761
[3,]  0.17839128  0.122139269  0.03668812

Desired output example:

>aligned_coords
$individual1
 , , rep1

            [,1]         [,2]         [,3]
 [1,] -0.1838745 -0.002361134  0.001343360
 [2,]  0.1717784 -0.065992358 -0.003245113
 [3,]  0.1701612  0.025533125 -0.068783434

 , , rep2

            [,1]        [,2]          [,3]
 [1,] -0.1844239  0.00147890  0.0001282347
 [2,]  0.1846240 -0.07485290 -0.0031490047
 [3,]  0.1681969  0.02331805 -0.0627923402

$individual2
 , , rep1

            [,1]         [,2]        [,3]
 [1,] -0.1439503  0.007217439  0.04066458
 [2,]  0.1154014 -0.009312094 -0.10422833
 [3,]  0.1673553  0.117909836  0.03056590

 , , rep2

             [,1]         [,2]        [,3]
 [1,] -0.14248417  0.004972831  0.03475940
 [2,]  0.09739824 -0.008022416 -0.09583761
 [3,]  0.17839128  0.122139269  0.03668812

...

Is this possible with lapply? I tried the following:

aligned_coords <- lapply(unaligned_coords, gpagen)$coords

But I got:

>aligned_coords
NULL

Upvotes: 0

Views: 54

Answers (1)

akrun
akrun

Reputation: 887213

We can use an anonymous function call to extract the coords after applying the gpagen function

library(geomorph)
lapply(unaligned_coords, function(x) gpagen(x)$coords)

data

set.seed(24)
unaligned_coords <- list(individual1 = array(rnorm(3 * 3 * 2, 60), 
  dim = c(3, 3, 2), dimnames = list(NULL, NULL, c("rep1", "rep2"))), 
 individual2 = array(rnorm(3 * 3 * 2, 60), dim = c(3, 3, 2), 
   dimnames = list(NULL, NULL, c("rep1", "rep2")) ) )

Upvotes: 0

Related Questions