0

My list has 12000 entries. Each entry consists of 16 columns and 8 rows. I would like to create a data frame for every single entry. I'm interested in 3 of the 16 columns (X,Y and Z coordinates)

I already tried this:

data_frame12000 <- as.data.frame(do.call(cbind, list_small_read_laz))

This and other functions only create one big data.frame with all the 16 columns for each entry.

Can anybody help me?

Thank You in advance!

Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29
Bodobert
  • 1
  • 1
  • 1
    Please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of the first couple of elements of your list so that possible answers can be tested. – thelatemail Nov 16 '22 at 22:36
  • How did you source this lidar data originally? i.e.,there might be other parts of your process worth mentioning prior to the unsatisfactory data_frame12000 result. – Chris Nov 17 '22 at 00:47
  • Greetings! Usually it is helpful to provide a minimally reproducible dataset for questions here so people can troubleshoot your problems (rather than a table or screenshot for example). One way of doing is by using the `dput` function on the data or a subset of the data you are using, then pasting the output into your question. You can find out how to use it here: https://youtu.be/3EID3P1oisg – Shawn Hemelstrand Nov 17 '22 at 01:09

2 Answers2

0

If I am correct, you have a list containing 12000 elements each cintaining a dataframe with 8r*16c. And I suppose the column names are the same for all list elements.

First select X, Y, Z columns from each entry element :

library(tidyverse) 
# assumming your list name is 'list_small_read_laz'
reduced_column <- map(list_small_read_laz,~ select(.,X,Y,Z))

Then combine all entries into a single dataframe:

df_reduced_column <- map_dfr(reduced_column, as.data.frame)

Hope this is what you are looking for.

S-SHAAF
  • 1,863
  • 2
  • 5
  • 14
  • Thank You so much. That really helped me. Now I am challenging the next question: I want to measure the fractal dimension via box.counting: box_counting(reduced_column[[1]]) This one is working good but I need the mean of all data frames I tried this to select more of the data frames but got an error message: mean(box_counting(reduced_column[[1:12062]])) ... Can You tell me how I can pick a row of data frames? Thank You! :) – Bodobert Nov 17 '22 at 01:32
  • One more thing: Is there a possibility to Select only Z values > 0.1? Maybe something like reduced_column <- map(list_small_read_laz,~ select(.,X,Y,Z > 0.1)) – Bodobert Nov 17 '22 at 01:36
  • you can do the same with the 'map' function:boxcounter <- map(reduced_column, box_counting) – S-SHAAF Nov 17 '22 at 01:50
  • And to keep only Z values > 0.1 : Z_keep <- map(reducd_column, ~ filter(. , Z > 0.1)). Hope it is helping. – S-SHAAF Nov 17 '22 at 01:57
  • Thank You very much! No I get an error message, because some of the data frames don't contain any values. Thats ok, but to compute the map function I have to delete those frames. Is there a function to delete those empty data frames? Thank You!! – Bodobert Nov 17 '22 at 11:03
  • This is a very basic but anyway I gave it here: you can delete from your list by element index, for example if you want to delete elements 1 and 3 do this: reducd_column[c(1, 3)] = NULL. Or you can give by the names : reducd_column[c("entry1","entry3")] = NULL. – S-SHAAF Nov 17 '22 at 12:12
  • Very kind! But how can I delete items with a specific value. Because of the filter some data frames don't contain enough values any more (items that are described with "list[0x3"] and "list[1x3"]). I need to delete those ones. Do you know any function for this kind of "filter"? – Bodobert Nov 17 '22 at 15:56
0

If you have a list of 12000 dataframes you can generate a list of dataframes with only the desired columns using lapply. Here is an example using mtcars:

cars1 <- mtcars
cars2 <- cars1
cars3 <- cars2
list1 <- list(cars1, cars2, cars3)

df_list <- lapply(list1, function(x) x[, c(2, 4, 6)]) # column numbers are used

final_df <- Reduce(rbind, df_list) # if you want all of the dataframes combined by rows
SteveM
  • 2,226
  • 3
  • 12
  • 16