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!
CodePudding user response:
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.
CodePudding user response:
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