-1

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
[[2]] 5.211102e-02 -0.0141554617 -0.0176815372 -2.861500e-02 -0.1155798724 0.0561973096 2.230498e-03 -0.0264566373 -0.0501298532
I then used this function to create 1000 distance matrices:

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.

KNN
  • 459
  • 4
  • 19
  • Are you asking for an example input into dist()? I'm trying to extract the last "row" of the distance matrix for all 1000 distance matrices which are all in a list (df). I'll see if I can modify this. – KNN May 09 '19 at 23:50
  • *"I now want to extract the last row from every list"* Do you mean the last row from every `dist`ance matrix? – Maurits Evers May 09 '19 at 23:57
  • Yes, exactly! I have 1000 nested distance matrices. If I don't convert my df to a matrix they are class "list" and typeof "list" so I thought I was using the correct terminology. Thanks. – KNN May 09 '19 at 23:59
  • I did not downvote your question; but I imagine that the downvote is due to your post not including any reproducible minimal sample data which is always a critical component of [good questions](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) asking how to do XYZ. I've posted an answer below (based on some minimal sample data), please take a look. – Maurits Evers May 10 '19 at 00:11
  • 1
    PS. Generally I agree with you in that downvotes can be difficult to interpret without any additional comments. However, the reason for downvotes are often very consistent: Lack of sample data, data/code as screenshots, no code attempt, multiple questions or no specific question. So I'd say that working through that list to see where your question falls short is usually a good starting point. If you've ticked all the boxes and still receive downvotes, that's when things get tricky. Either way, don't take DVs personal and do stick around. – Maurits Evers May 10 '19 at 00:14

1 Answers1

0

Here is an example using minimal sample data based on mtcars

lst <- lapply(1:3, function(x) dist(mtcars[, 1:3]))

We now extract the last row of every distance matrix from lst

lapply(lst, function(x) { mat <- as.matrix(x); mat[nrow(mat), ] })
#[[1]]
#          Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive
#         39.0532969          39.0532969          13.0751673         137.0145978
#  Hornet Sportabout             Valiant          Duster 360           Merc 240D
#        239.0487189         104.0715619         239.1388927          25.8745048
#           Merc 230            Merc 280           Merc 280C          Merc 450SE
#         19.8494332          46.6947535          46.7816203         154.9323723
#         Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental
#        154.9059392         154.9757400         351.1951025         339.2020047
#  Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla
#        319.0954246          43.7068644          46.1853873          51.4418118
#      Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28
#          0.9055385         197.1289172         183.1486828         229.1781185
#   Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa
#        279.0373452          42.4123803           4.6529560          27.4191539
#     Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E
#        230.1029335          24.1431150         180.1581527           0.0000000
#
#[[2]]
#          Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive
#         39.0532969          39.0532969          13.0751673         137.0145978
#  Hornet Sportabout             Valiant          Duster 360           Merc 240D
#        239.0487189         104.0715619         239.1388927          25.8745048
#           Merc 230            Merc 280           Merc 280C          Merc 450SE
#         19.8494332          46.6947535          46.7816203         154.9323723
#         Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental
#        154.9059392         154.9757400         351.1951025         339.2020047
#  Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla
#        319.0954246          43.7068644          46.1853873          51.4418118
#      Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28
#          0.9055385         197.1289172         183.1486828         229.1781185
#   Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa
#        279.0373452          42.4123803           4.6529560          27.4191539
#     Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E
#        230.1029335          24.1431150         180.1581527           0.0000000
#
#[[3]]
#          Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive
#         39.0532969          39.0532969          13.0751673         137.0145978
#  Hornet Sportabout             Valiant          Duster 360           Merc 240D
#        239.0487189         104.0715619         239.1388927          25.8745048
#           Merc 230            Merc 280           Merc 280C          Merc 450SE
#         19.8494332          46.6947535          46.7816203         154.9323723
#         Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental
#        154.9059392         154.9757400         351.1951025         339.2020047
#  Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla
#        319.0954246          43.7068644          46.1853873          51.4418118
#      Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28
#          0.9055385         197.1289172         183.1486828         229.1781185
#   Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa
#        279.0373452          42.4123803           4.6529560          27.4191539
#     Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E
#        230.1029335          24.1431150         180.1581527           0.0000000
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68