What is the best way to make nested lists with large datasets.
Currently I am adding the elements with a for but I don't know if it is the most efficient way to do it.
In the example below I want to fill a nested list with the taxonomic levels Domain> Kingdom> Phylum> Class> Order> Family> Genus> Species.
So the code currently goes through each taxonomic level collecting the information and filling in the list, but due to the amount of data, the process takes too long and I would like to know if there is a way to optimize the process.
The code is attached below. I appreciate any suggestions, comments, etc.
Thank you
data <- data.frame(Reino = c("reinoa","reinoa","reinob","reinoc"),
Filo = c("Filoa1","Filoa2","Filob","Filoc"),
Clase = c("Clasea1","Clase2","Claseb","Clasec"),
Orden = c("Ordena1","Ordena2","Ordenb","Ordenc"),
Familia = c("Familiaa1","Familiaa2","Familiab","Familiac"),
Genero = c("Generoa1","Generoa2","Generob","Generoc"),
Especie = c("Especiea1","Especiea2","Especieb","Especiec"))
for(i in unique(data$Reino)){
dftaxonomica[[i]] <- list()
print(i)
for(j in unique(data[data$Reino==i,]$Filo)){
dftaxonomica[[i]][[j]] <- list()
for(w in unique(data[data$Reino==i & data$Filo==j,]$Clase)){
dftaxonomica[[i]][[j]][[w]] <- list()
for(z in unique(data[data$Reino==i & data$Filo==j & data$Clase == w,]$Orden)){
dftaxonomica[[i]][[j]][[w]][[z]] <- list()
for(h in unique(data[data$Reino==i & data$Filo==j & data$Clase == w & data$Orden == z,]$Familia)){
dftaxonomica[[i]][[j]][[w]][[z]][[h]] <- list()
for(q in unique(data[data$Reino==i & data$Filo==j & data$Clase == w & data$Orden == z & data$Familia == h,]$Genero)){
dftaxonomica[[i]][[j]][[w]][[z]][[h]][[q]] <- list()
for(k in unique(data[data$Reino==i & data$Filo==j & data$Clase == w & data$Orden == z & data$Familia == h & data$Género == h,]$Especie)){
dftaxonomica[[i]][[j]][[w]][[z]][[h]][[q]][[k]] <- list()
}
}
}
}
}
}
}