Home > Mobile >  Naming sublists in R
Naming sublists in R

Time:09-02

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:

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))))

CodePudding user response:

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 ...
...
  • Related