[英]Assign, Save and Load a Sequence of Objects to a Sequence of Variables in R
[英]how to assign a sequence of computations to a sequence of variables in r
我想在循環中遇到一些棘手的挑戰,我想在循環中做得更快。 如何將一系列小型計算分配給變量序列? 例:
fex1 = rbind(ben1,mal1)
fex2 = rbind(ben2,mal2)
fex3 = rbind(ben3,mal3)
....
....
fex40 = rbind(ben40,mal40)
其中ben(i)和mal(i)是序列為1:40的7×13矩陣,而fex(i)也是變量名稱為1:40的序列。 基本上,我已將一些數據拆分為不同的折疊,並希望綁定拆分數據集的組合以執行其他一些計算。 我已經使用lapply遍歷rbind和其他函數,但是如何在矩陣序列上應用rbind之類的函數並將值也存儲在變量序列中來實現此任務呢?
謝謝。
您實際上應該在此處使用列表:
#
ben <- <list of all your ben's>
mal <- <list of all your mal's>
fex <- mapply(rbind, ben, mal)
# then just index using
fex[[i]]
如果必須具有單獨的變量,請使用assign
:
N <- 30 # however many of each `ben` and `mal` you have
for (i in N) {
bi <- paste0(ben, i)
mi <- paste0(mal, i)
fi <- paste0(fex, i)
assign(fi, rbind(get(bi), get(mi)))
}
注意將您的對象收集到一個列表中:
ben <- lapply(do.call(paste0, list("ben", 1:N)), get)
mal <- lapply(do.call(paste0, list("mal", 1:N)), get)
# Which can then be indexed by
ben[[7]]
mal[[12]] # etc
但是,您還應該嘗試將它們放在getgo的列表中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.