Mukhtar Abdi
Mukhtar Abdi

Reputation: 411

having issue to create one data.frame from a list of lists

I have a list called WI containing a list of two including S1 & S2, and each sub list further contains a list of 10 representing data.frames.

I need to create one dataframe from that lists. But I have an issue, I am getting a data.frame containing only one level called "ndvi.mean" for both S1 and S2, as I need ten levels include "gridded_idw2_sum" "Mada4_sum", "Mada22_sum", "Mada38_sum", "Mada53_sum", "GDD.AS_sum", "GDD.Ch_sum", "hum.AS.mean", "hum.Ch.mean", "ndvi.mean".

At the end I want get a data.frame containign five cols "year" "aggre_per" "value" "WI" ""Season"". The aggre_per column has to contain 10 levels "gridded_idw2_sum" "Mada4_sum", "Mada22_sum", "Mada38_sum", "Mada53_sum", "GDD.AS_sum", "GDD.Ch_sum", "hum.AS.mean", "hum.Ch.mean", and "ndvi.mean", and the Season column has to contain two levels S1 and S2.

Any solution for this issue please?

Here is my code

> names(WI)
[1] "S1" "S2"
> names(WI[[1]])
 [1] "gridded_idw2_sum" "Mada4_sum"        "Mada22_sum"       "Mada38_sum"      
 [5] "Mada53_sum"       "GDD.AS_sum"       "GDD.Ch_sum"       "hum.AS.mean"     
 [9] "hum.Ch.mean"      "ndvi.mean"       
> # Season 1
> for (i in 1:length(names(WI[[1]]))) {
+   
+   Ind <- names(WI[[1]])[i]
+   
+   data_s1 <- WI[["S1"]][[Ind]]
+   
+     
+   data_s1$WI <- Ind
+   data_s1$Season <- "S1"
+   
+ }
> 
> 
> # Season 2
> for (i in 1:length(names(WI[[1]]))) {
+   
+   Ind <- names(WI[[2]])[i]
+   
+   data_s2 <- WI[["S2"]][[Ind]]
+ 
+   data_s2$WI <- Ind
+   data_s2$Season <- "S2"
+   data_s1_s2  <- rbind(data_s1, data_s2)
+   
+ }
> head(data_s1_s2)
  year aggre_per value        WI Season
1 2001         3  0.44 ndvi.mean     S1
2 2002         3  0.28 ndvi.mean     S1
3 2003         3  0.40 ndvi.mean     S1
4 2004         3  0.31 ndvi.mean     S1
5 2005         3  0.40 ndvi.mean     S1
6 2006         3  0.41 ndvi.mean     S1
> #get the levels of `Season` col
> unique(data_s1_s2$Season)
[1] "S1" "S2"
> #the levels of `WI` col
> unique(data_s1_s2$WI)
[1] "ndvi.mean"

I cannot the produce the dput of my data because its too long. But, here is google drive link to access my data https://drive.google.com/file/d/1UJ88MRTfr_eZzAHmRw4w6fEc6ec3VGiU/view?usp=sharing.

Upvotes: 0

Views: 41

Answers (1)

DaveArmstrong
DaveArmstrong

Reputation: 22074

You could do it like this:

for(i in 1:length(WI[[1]])){
  WI[[1]][[i]]$name <- names(WI[[1]])[i]
  WI[[1]][[i]]$Season <- "S1"
}

for(i in 1:length(WI[[2]])){
  WI[[2]][[i]]$name <- names(WI[[2]])[i]
  WI[[2]][[i]]$Season <- "S2"
}

WI[[1]] <- dplyr::bind_rows(WI[[1]])
WI[[2]] <- dplyr::bind_rows(WI[[2]])
WI <- dplyr::bind_rows(WI)

table(WI$Season)
# S1   S2 
# 1120 1120 
table(WI$name)
# GDD.AS_sum       GDD.Ch_sum gridded_idw2_sum      hum.AS.mean      hum.Ch.mean       Mada22_sum       Mada38_sum 
#        224              224              224              224              224              224              224 
# Mada4_sum       Mada53_sum        ndvi.mean 
#        224              224              224 

Upvotes: 1

Related Questions