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

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

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

问题

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

环境数据框

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

生物指数数据框

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

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

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

然后,我执行了回归。

Ldf1 = list()
LBM1 = list()
for(i in seq_along(LT)){
LBM1[[i]] = betareg(LT[[i]][[1]] ~ LT[[i]][[i+1]])
Ldf1[[i]] = summary(LBM[[i]])
}

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

英文:

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

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

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.

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

Then, I performed the regression.

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

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.

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

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:

确定