Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Naming sublists in R

Suppose that I have this list that has 3 sublists, and each sublist carries 3 dataframes, it looks like this: (data at the end)

enter image description here

Then suppose that I have 2 vectors:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

j <- seq(10, 20, length.out = 3)
v <- seq(0, 1, length.out = 3)

I would like to make a for loop that names the lists and dataframes, I have managed to name the first-level lists with this for loop:

for(i in 1:length(a)){
  names(a)[i] <- paste("y0 =",j[i])
}

But I can’t figure out how to get to the dataframe name.

I have tried this but it doesn’t work. Should I try nested for loops? Is there a simpler way?

for(i in 1:length(a)){
  names(a)[i] <- paste("y0 =",j[i])
  names(a)[[i]][[i]] <- paste("sigma_e =", v[i])
}

Thanks in advance


Here is the data:

> dput(a)
list(list(structure(list(period = 1:10, y = c(NA, 10, 10, 10, 
10, 10, 10, 10, 10, 10)), class = "data.frame", row.names = c(NA, 
-10L)), structure(list(period = 1:10, y = c(NA, 10, 10.7793541570746, 
10.8146083527869, 10.8792522203673, 11.736784713809, 11.9672428168036, 
11.3347121995003, 10.9912857735535, 10.7684547885036)), class = "data.frame", row.names = c(NA, 
-10L)), structure(list(period = 1:10, y = c(NA, 10, 11.5587083141491, 
11.6292167055737, 11.7585044407346, 13.4735694276179, 13.9344856336071, 
12.6694243990006, 11.9825715471071, 11.5369095770071)), class = "data.frame", row.names = c(NA, 
-10L))), list(structure(list(period = 1:10, y = c(NA, 15, 15, 
15, 15, 15, 15, 15, 15, 15)), class = "data.frame", row.names = c(NA, 
-10L)), structure(list(period = 1:10, y = c(NA, 15, 15.7793541570746, 
15.8146083527869, 15.8792522203673, 16.736784713809, 16.9672428168036, 
16.3347121995003, 15.9912857735535, 15.7684547885036)), class = "data.frame", row.names = c(NA, 
-10L)), structure(list(period = 1:10, y = c(NA, 15, 16.5587083141491, 
16.6292167055737, 16.7585044407346, 18.4735694276179, 18.9344856336071, 
17.6694243990006, 16.9825715471071, 16.5369095770071)), class = "data.frame", row.names = c(NA, 
-10L))), list(structure(list(period = 1:10, y = c(NA, 20, 20, 
20, 20, 20, 20, 20, 20, 20)), class = "data.frame", row.names = c(NA, 
-10L)), structure(list(period = 1:10, y = c(NA, 20, 20.7793541570746, 
20.8146083527868, 20.8792522203673, 21.736784713809, 21.9672428168036, 
21.3347121995003, 20.9912857735535, 20.7684547885036)), class = "data.frame", row.names = c(NA, 
-10L)), structure(list(period = 1:10, y = c(NA, 20, 21.5587083141491, 
21.6292167055737, 21.7585044407346, 23.4735694276179, 23.9344856336071, 
22.6694243990006, 21.9825715471071, 21.5369095770071)), class = "data.frame", row.names = c(NA, 
-10L))))

>Solution :

We could use

anew <- setNames(lapply(a, function(asub) setNames(asub,
      paste("sigma_e=", v))), paste0("y0", j))

-checking

> str(anew)
List of 3
 $ y010:List of 3
  ..$ sigma_e= 0  :'data.frame':    10 obs. of  2 variables:
  .. ..$ period: int [1:10] 1 2 3 4 5 6 7 8 9 10
  .. ..$ y     : num [1:10] NA 10 10 10 10 10 10 10 10 10
  ..$ sigma_e= 0.5:'data.frame':    10 obs. of  2 variables:
  .. ..$ period: int [1:10] 1 2 3 4 5 6 7 8 9 10
  .. ..$ y     : num [1:10] NA 10 10.8 10.8 10.9 ...
  ..$ sigma_e= 1  :'data.frame':    10 obs. of  2 variables:
  .. ..$ period: int [1:10] 1 2 3 4 5 6 7 8 9 10
  .. ..$ y     : num [1:10] NA 10 11.6 11.6 11.8 ...
 $ y015:List of 3
  ..$ sigma_e= 0  :'data.frame':    10 obs. of  2 variables:
  .. ..$ period: int [1:10] 1 2 3 4 5 6 7 8 9 10
  .. ..$ y     : num [1:10] NA 15 15 15 15 15 15 15 15 15
  ..$ sigma_e= 0.5:'data.frame':    10 obs. of  2 variables:
  .. ..$ period: int [1:10] 1 2 3 4 5 6 7 8 9 10
  .. ..$ y     : num [1:10] NA 15 15.8 15.8 15.9 ...
  ..$ sigma_e= 1  :'data.frame':    10 obs. of  2 variables:
  .. ..$ period: int [1:10] 1 2 3 4 5 6 7 8 9 10
  .. ..$ y     : num [1:10] NA 15 16.6 16.6 16.8 ...
...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading