如何在R中对存储在列表中的数据框的列名执行循环。

huangapple go评论88阅读模式
英文:

How to do a loop over a colnames of data frames stored in a list in r

问题

我正在尝试对存储在列表上的多个数据框的列执行贝塔回归。列表中存储的数据框来自两个数据框,一个包含因变量,另一个包含自变量(环境因素)。随后,我将从多个表格获取的列粘贴到环境数据框以执行回归。

环境数据框

  1. Ev = data.frame(a = runif(20, 1, 999), b = runif(20, 1, 7000), c = runif(20, 1, 3000), d = runif(20, 1, 250))

生物指数数据框

  1. Index = data.frame(Pielou = runif(20, 0, 1), Simpson = runif(20, 0, 1), LCBD = runif(20, 0, 1), Q = runif(20, 0, 0.8), D = runif(20, 0, 0.6))

在这里,我将每个生物列粘贴到环境数据框中。

  1. LT = list()
  2. for(i in seq_along(Index)){
  3. LT[[i]] = data.frame(Index[,i], Ev)
  4. }

然后,我执行了回归。

  1. Ldf1 = list()
  2. LBM1 = list()
  3. for(i in seq_along(LT)){
  4. LBM1[[i]] = betareg(LT[[i]][[1]] ~ LT[[i]][[i+1]])
  5. Ldf1[[i]] = summary(LBM[[i]])
  6. }

然而,循环只为我提供了第一个数据框的四个结果。

英文:

I'm seeking to conduct a beta regression across columns of multiple data frames stored on a list. The data frames stored in the list are from two data frames, one containing dependent factors and another containing independent variables (environmental). Subsequently, I pasted a column to environmental data frames acquired from multiple tables to carry out the regression.

Environmental data frame

  1. Ev = data.frame(a = runif(20,1,999), b = runif(20,1,7000), c = runif(20,1,3000), d = runif(20, 1, 250)))

Biotic index data frame

  1. Index = data.frame(Pielou = runif(20,0,1), Simpson = runif(20,0,1), LCBD = runif(20, 0, 1), Q = runif(20, 0, 0.8), D = runif(20,0,0.6))

Here I pasted each Bio column in environmental df.

  1. LT = list()
  2. for(i in seq_along(Index)){
  3. LT[[i]] = data.frame(Index[,i], Ev)
  4. }

Then, I performed the regression.

  1. Ldf1 = list();LBM1 = list()
  2. for(i in seq_along(LT)){
  3. LBM1[[i]] = betareg(LT[[i]][[1]] ~ LT[[i]][[i+1]])
  4. Ldf1[[i]] = summary(LBM[[i]])
  5. }

However, the loop only provided me with four results of the first data frame.

如何在R中对存储在列表中的数据框的列名执行循环。

答案1

得分: 1

Ldf1 = vector("list", length(LT) * length(Ev))
LBM1 = vector("list", length(LT) * length(Ev))
idx = 1
for(i in seq_along(LT)){
for(j in seq_along(Ev)){
LBM1[[idx]] = betareg(LT[[i]][1] ~ LT[[i]][[j]])
Ldf1[[idx]] = summary(LBM1[[idx]])
idx = idx+1
}
}

英文:

I hope this works for you.

  1. Ldf1 = vector("list", length(LT) * length(Ev))
  2. LBM1 = vector("list", length(LT) * length(Ev))
  3. idx = 1
  4. for(i in seq_along(LT)){
  5. for(j in seq_along(Ev)){
  6. LBM1[[idx]] = betareg(LT[[i]][[1]] ~ LT[[i]][[j]])
  7. Ldf1[[idx]] = summary(LBM1[[idx]])
  8. idx = idx+1
  9. }
  10. }

huangapple
  • 本文由 发表于 2023年2月18日 15:13:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定