假設我有這個包含 3 個子串列的串列,每個子串列攜帶 3 個資料幀,它看起來像這樣:(資料在末尾)
然后假設我有 2 個向量:
j <- seq(10, 20, length.out = 3)
v <- seq(0, 1, length.out = 3)
我想創建一個命名串列和資料框的 for 回圈,我已經設法用這個 for 回圈命名了第一級串列:
for(i in 1:length(a)){
names(a)[i] <- paste("y0 =",j[i])
}
但我不知道如何獲取資料框名稱。
我已經嘗試過了,但它不起作用。我應該嘗試嵌套 for 回圈嗎?有沒有更簡單的方法?
for(i in 1:length(a)){
names(a)[i] <- paste("y0 =",j[i])
names(a)[[i]][[i]] <- paste("sigma_e =", v[i])
}
提前致謝
這是資料:
> 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))))
uj5u.com熱心網友回復:
我們可以使用
anew <- setNames(lapply(a, function(asub) setNames(asub,
paste("sigma_e=", v))), paste0("y0", j))
-檢查
> 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 ...
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/505276.html
上一篇:密碼生成器代碼回傳空輸出