Luuk
Luuk

Reputation: 75

Changing names of lists within a list

I have the following data frame that I split into three lists based on the differences in Model name ("Bristol_A","Bristol_B", and "Bristol_C")

   a=c("Bristol_A","R1",358723.0,171704.0,1.0,36.818500,4.0222700,1.38895000)
   b=c("Bristol_A","R2",358723.0,171704.0,2.6,36.447300,4.0116100,1.37479000)
   c=c("Bristol_A","R3",358723.0,171704.0,5.0,35.615400,3.8092600,1.34301000)
   d=c("Bristol_B","R1",358723.0,171704.0,1.0,39.818300,2.4475600,1.50384000)
   e=c("Bristol_B","R2",358723.0,171704.0,2.6,39.391600,2.4209900,1.48754000)
   f=c("Bristol_B","R3",358723.0,171704.0,5.0,38.442700,2.3618400,1.45126000)
   g=c("Bristol_C","R1",358723.0,171704.0,1.0,31.246400,2.2388000,1.30652000)
   h=c("Bristol_C","R2",358723.0,171704.0,2.6,30.911600,2.2144800,1.29234000)
   i=c("Bristol_C","R3",358723.0,171704.0,5.0,30.166700,2.1603000,1.26077000)
   df=data.frame(a,b,c,d,e,f,g,h,i)
   df=t(df)
   df=data.frame(df)
   col_list=list("Model","Receptor.name","X(m.)","Y(m.)","Z(m.)",
          "nox","PM10","PM2.5")
   colnames(df)=col_list
   data_Report_list=split.data.frame(df, df$Model)
   list2env(split(df, df$Model), envir = .GlobalEnv) 

What I hope to achieve is to rename the individual lists to "Model_A" (and subsequent increases to "Model_B", "Model_C" etc.

My current solution (which doesn't work) was:

    names(data_Report_list[1])="Model A"
    names(data_Report_list[2])="Model B"
    names(data_Report_list[3])="Model C"

However, besides it not working, I need the script to be automated so I don't want to write out each line individually, and instead have R recognize that there a X amount of files in this list, therefore it change the same amount of names in the list. E.g., if there are 6 models in a list, R will name them as "Model_A" to "Model_F". Is this even possible?

--------------------------------EDIT----------------------------------

the output of akrun's comment is this:

     $`Bristol_A`
         Model_A Receptor.name  X(m.)  Y(m.) Z(m.)     nox    PM10   PM2.5
    a Bristol_A            R1 358723 171704     1 36.8185 4.02227 1.38895
    b Bristol_A            R2 358723 171704   2.6 36.4473 4.01161 1.37479
    c Bristol_A            R3 358723 171704     5 35.6154 3.80926 1.34301 

but what I need is this (the first line changes from "Bristol_A" to "Model_A")

      $`Model_A`
        Model_A Receptor.name  X(m.)  Y(m.) Z(m.)     nox    PM10   PM2.5
      a Bristol_A            R1 358723 171704     1 36.8185 4.02227 1.38895
      b Bristol_A            R2 358723 171704   2.6 36.4473 4.01161 1.37479
      c Bristol_A            R3 358723 171704     5 35.6154 3.80926 1.34301

Upvotes: 1

Views: 311

Answers (1)

akrun
akrun

Reputation: 886948

We can loop through the list with Map and assign the first column name

nm1 <- paste0("Model_", LETTERS[seq_along(data_Report_list)])
data_Report_list <- Map(function(x, y) {names(x)[1] <- y; x}, 
            data_Report_list, nm1)
names(data_Report_list) <- nm1
data_Report_list[1]
#$Model_A
#    Model_A Receptor.name  X(m.)  Y(m.) Z(m.)     nox    PM10   PM2.5
#a Bristol_A            R1 358723 171704     1 36.8185 4.02227 1.38895
#b Bristol_A            R2 358723 171704   2.6 36.4473 4.01161 1.37479
#c Bristol_A            R3 358723 171704     5 35.6154 3.80926 1.34301

Upvotes: 1

Related Questions