I'm sure this question is a duplicate but I can't find the correct solution. I have a large nested list, which is actually a list of class 'dist' outputs from lapply dist(). I now want to extract the last row from every list (since I only cared about the distance between the last rows of my original lists and all other rows in that same nested list. I could convert this to a matrix, but that hasn't really helped me.
From a few related posts Extract then row.bind data.frames from nested lists and R - Extracting information from list of lists of data.frames and others I got closer.
I can extract the first element (corresponding to first columns) from every nested list using this, but I want rows:
sapply(df,'[[',1)
This does something but not sure what exactly honestly:
lapply(df, function(x) lapply(x, '[' , drop = FALSE))
My original input into dist() was as follows. I have a nested list of 1000 lists that have x,y,z coordinates (there are many rows, here's an example):
[[1]]
x y z
-5.924184e-02 -0.0101882607 0.0017658100
-8.884432e-02 0.0454085919 -0.1072237461
1.495407e-02 0.0592509097 -0.0107400982
I then used this function to create 1000 distance matrices:
[[2]]
5.211102e-02 -0.0141554617 -0.0176815372
-2.861500e-02 -0.1155798724 0.0561973096
2.230498e-03 -0.0264566373 -0.0501298532
lapply(orig.df[1:1000],dist) -> df
Here's the top output from str(df)
str(df)
List of 1000
$ :Class 'dist' atomic [1:2628] 0.126 0.102 0.136 0.104 0.139 ...
.. ..- attr(*, "Size")= int 73
.. ..- attr(*, "Labels")= chr [1:73] "2" "8" "10" "14" ...
.. ..- attr(*, "Diag")= logi FALSE
.. ..- attr(*, "Upper")= logi FALSE
.. ..- attr(*, "method")= chr "euclidean"
.. ..- attr(*, "call")= language FUN(x = X[[i]])
Basically what I now want to do is this:
df[1:1000][nrow(df),]
For all 1000 nested lists (distance matrices), take the last "row" and all "columns". This is a distance matrix of class dist. Seems simple enough but is giving me a hard time. Thanks for the help.